列表到JSON对象

时间:2015-10-05 12:54:29

标签: python json

我正在以这种形式返回数据:

['{\n  "data": {\n    "promoter": {\n      "instagram_id": 123,\n      "instagram_name": "user",\n      "instagram_token": "\\"123\\"",\n      "location": "sydney",\n      "profile_pic": "profile/pic.jpg",\n      "user_id": 1,\n      "username": "my_user_name"\n    },\n    "success": true\n  }\n}']

我很困惑如何将其转换为JSON object以便我可以这样做:

json_obj['data']['promoter']['instagram_id']

我尝试使用listJSON String转换为dumps,然后使用loads,但无济于事。

对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您应该只使用json.loads转换列表的第一个元素。演示:

>>> data = ['{\n  "data": {\n    "promoter": {\n      "instagram_id": 123,\n      "instagram_name": "user",\n      "instagram_token": "\\"123\\"",\n      "location": "sydney",\n      "profile_pic": "profile/pic.jpg",\n      "user_id": 1,\n      "username": "my_user_name"\n    },\n    "success": true\n  }\n}']
>>> import json
>>> json_obj = json.loads(data[0])
>>> json_obj['data']['promoter']['instagram_id']
123