[英]Converting JSON objects in to dictionary in python
I have string of data basically which has a objects with in the objects.. 我有基本的数据串,在对象中有一个对象..
{"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", group:{"id": "XXXX"}}}. You can check this format using "http://chris.photobooks.com/json/default.html" site.
No my requirement is to convert this to JSON objects as a dictionary. 我没有要求将此转换为JSON对象作为字典。 I have tried below way 我试过以下方式
import json
JSON_Datalist = '{"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", group:{"id": "XXXX"}}}'
the_dict = json.loads(JSON_DataList)
but the_dict gives only left side values, no right values... 但the_dict只给出左侧值,没有正确的值...
In the same way if string has a format.. 以同样的方式,如果字符串有格式..
"[{"sample": false, "radop": null, "view": true, "Example1": null}, {"noMarket": false, "Example2": null}]"
and following the same code. 并遵循相同的代码。
JSON_Datalist = '[{"sample": false, "radop": null, "view": true, "Example1": null}, {"noMarket": false, "Example2": null}]'
the_dict = json.loads(JSON_DataList)
it gives the dictionary of length of 2, and this is what expected... 它给出了长度为2的字典,这就是预期...
Can any please help me out in first case how can I get a dictionary... 在第一种情况下我可以帮助我如何获得字典...
I found two errors in your first example: 我在你的第一个例子中发现了两个错误:
group
in your stringified (Json) version of your dict. 您的字典(Json)版本中有一个group
。 This should be a "group"
(with quotes). 这应该是一个"group"
(带引号)。 JSON_Datalist
≠ JSON_DataList
(lowercase vs. capital L). JSON_Datalist
≠ JSON_DataList
(小写与大写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'}
after fix the problem group should be "group", below code can meet your requirement 修复后问题组应该是“组”,下面的代码可以满足您的要求
json_data={"id":"XXXX", "name": "xyz", "user" : { "id": "XXXX", "username":"XYZ", "group":{"id": "XXXX"}}}
data = json.dumps(json_data)
json_to_python = json.loads(data)
print (json_to_python)
{'id': 'XXXX', 'name': 'xyz', 'user': {'id': 'XXXX', 'username': 'XYZ', 'group': {'id': 'XXXX'}}}
I have figured out how to generate the_dict['user']['group']['id']
dynamically through Python's eval
expression. 我已经想出如何通过Python的eval
表达式动态生成the_dict['user']['group']['id']
。
Keys is the input from the user, separated by :
. 键是用户的输入,由以下各项分隔:
。 Example: user:group:id
. 示例: user:group:id
。
CODE: 码:
RefCount = 0
RespDict = json.loads(JSON_Datalist.content) #convert strings to Java object using JSON
SplitKeys = Keys.split(":")
KeyCount = len(SplitKeys)
CommandText = "RespDict" #To setup command line based on keys information
while (RefCount <KeyCount):
CommandText = CommandText+"['"+SplitKeys[RefCount]+"']"
RefCount = RefCount + 1
print CommandText
print eval(CommandText) #Final key value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.