[英]parsing json into dictionary in python
I have the following json which I am trying to parse into a dictionary so that I get just the queries in the "Queries" that is query 1 and query 2 我尝试将以下json解析为字典,以便仅在查询1和查询2的“查询”中获得查询
[
{ "A": "xyz",
"B": "this is xyz",
"Queries": [
"Query 1",
"Query 2"
]
}
]
I am using: 我在用:
import json
js=open('C://localpath//files.json')
data=json.load(js) //assuming that json.load() will make data as dictionary correct me if i am wrong
data.get("Queries")
ives the following error data.get("Queries")
产生以下错误
Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'get'
追溯(最近一次通话最近):AttributeError:“ list”对象中没有属性“ get”的文件“”,第1行
How do I get data to be a dictionary and not a list and use "Queries" to just get the queries out of json, I don't want to convert it to a list. 我如何将数据变成字典而不是列表,并使用“查询”将查询从json中取出,我不想将其转换为列表。 Is there a way to directly treat it as a dictionary?
有没有办法直接将其视为字典?
try the following 尝试以下
js = open('C:/temp/json.txt').read()
data = json.loads(js)
data[0]['A']
u'xyz'
data
is a list of dictionaries. data
是词典列表。 You would need to index the dictionary out of the list 您需要将字典从列表中索引出来
data[0].get('Queries')
Or if there are multiple dictionaries, you could use a list comprehension to get the queries from each dictionary 或者,如果有多个字典,则可以使用列表推导来从每个字典中获取查询
[d.get('Queries') for d in data]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.