在python中将JSON对象转换为字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19110407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:53:28  来源:igfitidea点击:

Converting JSON objects in to dictionary in python

pythonjson

提问by Guruswamy B M

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. I have tried below way

没有我的要求是将其转换为 JSON 对象作为字典。我试过以下方式

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...

任何人都可以在第一种情况下帮助我如何获得字典...

回答by Alfe

I found two errors in your first example:

我在你的第一个例子中发现了两个错误:

  1. You have a groupin 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).
  1. group的字符串化 (Json) 版本的 dict 中有一个。这应该是一个"group"(带引号)。
  2. 你拼错了你的变量;JSON_DatalistJSON_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'}

回答by Guruswamy B M

I have figured out how to generate the_dict['user']['group']['id']dynamically through Python's evalexpression.

我已经弄清楚如何the_dict['user']['group']['id']通过Python的eval表达式动态生成。

Keysis the input from the user, separated by :. Example: user:group:id.

Keys是来自用户的输入,以 分隔:。例子: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

回答by xiyurui

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'}}}