Converting JSON objects in to dictionary in python

前端 未结 3 1003
余生分开走
余生分开走 2020-12-17 17:30

I have string of data basically which has a objects with in the objects..

{\"id\":\"XXXX\", \"name\": \"xyz\", \"user\" : { \"id\": \"XXXX\", \"username\":\"         


        
3条回答
  •  盖世英雄少女心
    2020-12-17 18:16

    I found two errors in your first example:

    1. You have a group in your stringified (Json) version of your dict. This should be a "group" (with quotes).
    2. You misspelled your variable; JSON_DatalistJSON_DataList (lowercase vs. capital L).

    After fixing both, I had no problems anymore:

    >>> JSON_Datalist = '{"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", "group":{"id": "XXXX"}}}'
    >>> the_dict = json.loads(JSON_Datalist)
    >>> the_dict
    {u'user': {u'username': u'XYZ', u'group': {u'id': u'XXXX'}, u'id': u'XXXX'}, u'id': u'XXXX', u'name': u'xyz'}
    

提交回复
热议问题