Skip to content Skip to sidebar Skip to footer

Python Json, Unnecessary Slashes

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() f

Solution 1:

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.

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}

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


Solution 2:

The easiest way is this:

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

jsonString = json.dumps(list)

jsonlist = json.loads(jsonString)

this aways work for me.


Post a Comment for "Python Json, Unnecessary Slashes"