Python json,不必要的斜杠 [英] Python json, unnecessary slashes

查看:122
本文介绍了Python json,不必要的斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的服务器端应用程序,我使用内置的模块"json"来创建客户端的答案.

I am creating a simple server side app, i use built in module "json" for creating an answer to client.

if isinstance(obj, (list, tuple)):
    return json.dumps({key: [o.to_json() for o in obj if
                             isinstance(o, JsonAble)]}, ensure_ascii=False)

我的班级在哪里

class ArtistSearchResult(JsonAble):

    def to_json(self):
        return json.dumps({'name': self.name, 'descr': self.descr,
                           "url": self.url, 'genres': self.genres},
                          allow_nan=False, ensure_ascii=False)

它可以工作,但是作为回应,我有一个不必要的斜杠:

It works, but in response i have an unnecessary slashes:

{"responce": ["{\"url\": \"/music/Lindsey+Buckingham\", \"genres\": [\"singer-songwriter\"], \"name\": \"Lindsey Buckingham\", \"descr\": \"Lindsey Adams Buckingham (born October 3, 1949) is an American guitarist, singer, composer and producer, most notable for being the...…read more\"}", "{\"url\": \"/music/Lindsey+Stirling\", \"genres\": [\"violin\"], \"name\": \"Lindsey Stirling\", \"descr\": \"Lindsey Stirling (b. 21 September 1986 in Orange County, California) is a violinist, dancer and composer based in Utah.…read more\"}", "{\"url\": \"/music/Graham+Lindsey\", \"genres\": [\"alt-country\"], \"name\": \"Graham Lindsey\", \"descr\": \"Graham Lindsey is a performing songwriter, an americana singer of subtle, stark ballads and waltzes of love and murder. A member of the...…read more\"}", "{\"url\": \"/music/Lindsey+Haun\", \"genres\": [\"country\"], \"name\": \"Lindsey Haun\", \"descr\": \"Lindsey Haun (born November 21, 1984 in Los Angeles, California) is an American actress and country music singer. Haun started acting...…read more\"}", "{\"url\": \"/music/Ryan+Lindsey\", \"genres\": [\"oklahoma\"], \"name\": \"Ryan Lindsey\", \"descr\": \"Ryan Lindsey is a singer-songwriter based in Norman, Oklahoma. He started as an entertainer early on making movies with his brothers and...…read more\"}", "{\"url\": \"/music/+noredirect/Tyler+Ward+&+Lindsey+Stirling\", \"genres\": [\"pop\"], \"name\": \"Tyler Ward & Lindsey Stirling\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Megan+Nicole+&+Lindsey+Stirling\", \"genres\": [\"pop\"], \"name\": \"Megan Nicole & Lindsey Stirling\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Lindsey+Pavao\", \"genres\": [\"the voice\"], \"name\": \"Lindsey Pavao\", \"descr\": \"Lindsey Pavao is an American singer from Sacramento, California. She was one of the contestants of NBC singing reality show, The Voice...…read more\"}", "{\"url\": \"/music/Steve+Forte+Rio+Feat.+Lindsey+Ray\", \"genres\": [\"house\"], \"name\": \"Steve Forte Rio Feat. Lindsey Ray\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Peter+Hollens+&+Lindsey+Stirling\", \"genres\": [\"ost\"], \"name\": \"Peter Hollens & Lindsey Stirling\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Steve+Forte+Rio%2FLindsey+Ray\", \"genres\": [], \"name\": \"Steve Forte Rio/Lindsey Ray\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Kurt+Schneider+&+Aim%C3%A9e+Proal+&+Lindsey+Stirling\", \"genres\": [\"female vocalist\"], \"name\": \"Kurt Schneider & Aimée Proal & Lindsey…\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Eppic+feat.+Lindsey+Stirling\", \"genres\": [\"instrumental\"], \"name\": \"Eppic feat. Lindsey Stirling\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Lindsey+Stirling+and+Shaun+Barrowes\", \"genres\": [\"instrumental\"], \"name\": \"Lindsey Stirling and Shaun Barrowes\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Lindsey+Lin%27s\", \"genres\": [\"zouk\"], \"name\": \"Lindsey Lin's\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Lindsey+Ray\", \"genres\": [\"pop\"], \"name\": \"Lindsey Ray\", \"descr\": \"At the tender age of five, Lindsey Ray declared to her family, \\\"When I grow up, I'm going to be a singer.\\\" The passion was in...…read more\"}", "{\"url\": \"/music/Tyler+Ward,+Chester+See+&+Lindsey+Stirling\", \"genres\": [\"pop\"], \"name\": \"Tyler Ward, Chester See & Lindsey…\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Patrick+Lindsey\", \"genres\": [\"techno\"], \"name\": \"Patrick Lindsey\", \"descr\": \"Born in Bad Nauheim near Frankfurt, Patrick Lindsey was one of the founders of the much respected label Kanzleramt in 1993. Later Patrick...…read more\"}", "{\"url\": \"/music/Steve+Forte+Rio;Lindsey+Ray\", \"genres\": [], \"name\": \"Steve Forte Rio;Lindsey Ray\", \"descr\": \"We don’t have a description for this artist yet\"}", "{\"url\": \"/music/Lindsey+Woolsey\", \"genres\": [\"pop songs\"], \"name\": \"Lindsey Woolsey\", \"descr\": \"Lindsey Woolsey are Elle Osborne and Melanie Wilson. They met in a hotel lobby in East London in 2004 and have been collaborating with...…read more\"}"]}

为什么它们出现在我的答案中?

Why they emerge in my answer?

推荐答案

您的to_json()方法返回包含JSON的字符串. json.dumps()然后继续转义引号,引号是JSON中的特殊字符.

Your to_json()method returns a string that contains JSON. json.dumps() then goes on to escape the quotes, which are a special character in JSON.

您的to_json()应该返回dict,然后可以由json.dumps()

Your to_json() should return a dict, which can then be encoded by json.dumps()

class ArtistSearchResult(JsonAble):

    def to_json_dict(self):
        return {'name': self.name, 'descr': self.descr,
                'url': self.url, 'genres': self.genres}

一种更好但更复杂的方法可能是编写自定义 JSONEncoder 子类.

A better, but more complicated way would probably be to write a custom JSONEncoder subclass.

这篇关于Python json,不必要的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆