Python json,不必要的斜杠

Python json,不必要的斜杠,python,json,python-2.7,Python,Json,Python 2.7,我正在创建一个简单的服务器端应用程序,我使用内置模块“json”创建对客户端的应答 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(JsonAbl

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

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)
它可以工作,但作为回应,我有一个不必要的斜杠:

{"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\"}"]}
为什么它们出现在我的答案中?

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

您的
to_json()
应该返回一个
dict
,然后可以通过
json.dumps()对其进行编码


更好但更复杂的方法可能是编写自定义子类。

最简单的方法是:

//convert list to string and then load it as json

jsonString = json.dumps(list)

jsonlist = json.loads(jsonString)

这对我来说很有用。

提示:
.to_json()
json.dumps()
都有什么作用?只是标准库惩罚你写“responce”。这些都不是多余的。它们在字符串中转义引号。谢谢。这是我正在寻找的。这是不必要的昂贵。
//convert list to string and then load it as json

jsonString = json.dumps(list)

jsonlist = json.loads(jsonString)