2013-06-27 3 views
1

나는 간단한 서버 측 애플 리케이션을 만들고 있는데, 나는 클라이언트에 대한 대답을 만들기 위해 "json"모듈을 내장하고있다.파이썬 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

표준 라이브러리가 "응답"작성에 대해 당신을 처벌하는 것입니다. –

+0

이것들은 불필요하지 않습니다. 그들은 문자열 안에서 따옴표를 이스케이프합니다. –

답변

2

to_json() 메서드는 JSON이 포함 된 문자열을 반환합니다. json.dumps() 그러면 JSON의 특수 문자 인 따옴표를 이스케이프 처리합니다.

귀하의 to_json()json.dumps()

class ArtistSearchResult(JsonAble): 

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

에 의해 다음 인코딩 할 수있는 dict를 반환해야 더 좋은,하지만 더 복잡한 방법은 아마 사용자에게 JSONEncoder 서브 클래스를 작성하는 것입니다. 나를 위해

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

jsonString = json.dumps(list) 

jsonlist = json.loads(jsonString) 

이가는 요리 작업 :

+0

감사합니다. 그것은 내가 찾고 있습니다. – Vetalll

0

가장 쉬운 방법이있다.