Python-搜索JSON [英] Python - Searching JSON
本文介绍了Python-搜索JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JSON输出如下:
I have JSON output as follows:
{
"service": [{
"name": ["Production"],
"id": ["256212"]
}, {
"name": ["Non-Production"],
"id": ["256213"]
}]
}
我希望找到所有包含非生产"作为名称的ID.
I wish to find all ID's where the pair contains "Non-Production" as a name.
我一直在考虑执行循环检查的过程,例如:
I was thinking along the lines of running a loop to check, something like this:
data = json.load(urllib2.urlopen(URL))
for key, value in data.iteritems():
if "Non-Production" in key[value]: print key[value]
但是,我似乎无法从服务"树中获取名称和ID,它返回:
However, I can't seem to get the name and ID from the "service" tree, it returns:
if "Non-Production" in key[value]: print key[value]
TypeError: string indices must be integers
假设:
- JSON是固定格式,无法更改
- 我没有root访问权限,并且无法安装任何其他软件包
本质上,目标是以最佳方式获取非生产服务"的ID列表.
Essentially the goal is to obtain a list of ID's of non production "services" in the most optimal way.
推荐答案
在这里:
data = {
"service": [
{"name": ["Production"],
"id": ["256212"]
},
{"name": ["Non-Production"],
"id": ["256213"]}
]
}
for item in data["service"]:
if "Non-Production" in item["name"]:
print(item["id"])
这篇关于Python-搜索JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文