Python json,不必要的斜线

标签 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)在列表中的位置

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() 编码

class ArtistSearchResult(JsonAble):

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

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

关于Python json,不必要的斜线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17336082/

相关文章:

python - 在 python 2.7 windows 中安装请求模块

python - 给定一定的比例,我如何调整图像的大小?

python - 从 SQLalchemy 中的自引用表创建树

python - matplotlib 或 seaborn 中的 utf-8 字符打印

javascript - 投票功能 "No Eintrag matches the given query"

json - 无法从 serde 导出和使用特征反序列化

python-2.7 - 无法在 ubuntu 16.04 中安装带有补丁 qt 的 wkhtmltopdf

javascript - 在 TypeScript 中向 JavaScript 对象添加数据

ios - Apple Push Notification Service-在消息文本中使用£和€字符

python - 在 Python 中动态创建函数和线程