2013-06-27 31 views
1

我正在創建一個簡單的服務器端應用程序,我使用內置模塊「json」來創建客戶端答案。Python 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\"}"]} 

爲什麼他們在我的答案出現嗎?

+4

提示:.to_json()和json.dumps()都做了什麼? –

+3

這只是標準庫懲罰你編寫「響應」。 – 2013-06-27 06:40:04

+0

那些沒有必要。他們在字符串中轉義引號。 –

回答

2

您的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子類。

+0

謝謝。這是我在尋找。 – Vetalll

0

最簡單的方法是這樣的:

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

jsonString = json.dumps(list) 

jsonlist = json.loads(jsonString) 

這對我來說跳投工作。