Convert unicode json to normal json in python

后端 未结 4 1246
谎友^
谎友^ 2020-12-19 02:28

I got the following json: {u\'a\': u\'aValue\', u\'b\': u\'bValue\', u\'c\': u\'cValue\'} by doing request.json in my python code. Now, I want to c

相关标签:
4条回答
  • 2020-12-19 02:52

    For python 2.x

    import yaml
    import json
    json_data = yaml.load(json.dumps(request.json()))
    

    Now this json_data can be used as a json and can have list of json as well.

    0 讨论(0)
  • 2020-12-19 02:52

    You can use a list comprehension to encode all of the keys and values as ascii like this:

    dict([(k.encode('ascii','ignore'), v.encode('ascii','ignore')) for k, v in dct.items()])
    

    Note: There generally isn't much benefit to not having your data in unicode, so unless you have a specific reason not to have it in unicode, then I would leave it.

    0 讨论(0)
  • 2020-12-19 02:53

    A library available in PyPi may be helpful, see: unidecode.

    It is intended to transform European characters with diacriticals (accents) to their base ASCII characters, but it does just as well when the unicode character is already in the ASCII range.

    from unicode import unidecode
    
    def fUnUn(sOrU):
        return unidecode(sOrU).encode('ascii') if type(sOrU) is unicode else sOrU
    
    sASCII = fUnUn(u'ASCII')
    
    0 讨论(0)
  • 2020-12-19 03:02

    {u'a': u'aValue', u'b': u'bValue', u'c': u'cValue'} is a dictionary which you are calling as unicode json. Now, in your language if you want a regular json from this then just do something like this:

    x={u'a': u'aValue', u'b': u'bValue', u'c': u'cValue'}
    y=json.dumps(x)
    print y
    

    The output will be {"a": "aValue", "c": "cValue", "b": "bValue"}

    0 讨论(0)
提交回复
热议问题