简体   繁体   English

遍历json列表并使用Python获取特定的键和值

[英]Iterate through a json list and get specific key and value with Python

I have a JSON list like this (it was a JSON response, the below is after i did json.loads ) 我有一个这样的JSON列表(这是一个JSON响应,以下是在我执行json.loads

[{'status': 'ok', 'slot': None, 'name': 'blah', 'index': 0, 'identify': 'off', 
'details': None, 'speed': None, 'temperature': None}, {'status': 'ok', 'slot': 
None, 'name': 'blah0', 'index': 0, 'identify': 'off', 'details': None, 
'speed': None, 'temperature': None}, {'status': 'ok', 'slot': None, 'name': 
'blah1', 'index': 1, 'identify': 'off', 'details': None, 'speed': None, 
'temperature': None}, {'status': 'ok', 'slot': None, 'name': 'blah2', 
'index': 2, 'identify': 'off', 'details': None, 'speed': None, 'temperature': 
None}, {'status': 'ok', 'slot': None, 'name': 'blah3', 'index': 3,
'identify': 'off', 'details': None, 'speed': None, 'temperature': None}]

I want to get both name and the status of the list, if name='blah' or 'blah0' or 'blah1' or 'blah2' or 'blah3' Essentially, for all the matches i want to store all the name and status in separate variables to use it elsewhere. 我想这两个namestatus列表,如果name='blah' or 'blah0' or 'blah1' or 'blah2' or 'blah3'从本质上讲,所有我想要的比赛来存储所有的namestatus在单独的变量中以在其他地方使用。 (can be dynamically creating variables or statically assigning them will also work for me) (可以动态创建变量或静态分配它们也对我有用)

I tried this, but doesn't seems to work the way i want. 我尝试了这个,但似乎并没有按照我想要的方式工作。

for value in data:
   if value['name'] in ['blah', 'blah0', 'blah1', 'blah2', 'blah3']:
        print(value['name'], value['status'])

This prints out the name and status as a string one line below the other. 这将namestatus作为字符串打印在另一行的下方。 But i want each name and status to be assigned to a variable so i can use it later. 但我希望将每个名称和状态分配给一个变量,以便以后使用。 Any help is much appreciated! 任何帮助深表感谢!

You don't really want dynamic variables, but you can use a list comprehension. 您实际上并不需要动态变量,但是可以使用列表推导。 You should also take advantage of constant-cost set membership test: 您还应该利用固定成本集合成员资格测试:

keep = set(['blah', 'blah0', 'blah1', 'blah2', 'blah3'])
result = [(value['name'], value['status']) for value in data if value['name'] in keep]
print(result)

Output: 输出:

[('blah', 'ok'),
 ('blah0', 'ok'),
 ('blah1', 'ok'),
 ('blah2', 'ok'),
 ('blah3', 'ok')]

If you want a dictionary: 如果您想要字典:

keep = set(['blah', 'blah0', 'blah1', 'blah2', 'blah3'])
result = {value['name']: value['status'] for value in data if value['name'] in keep}
print(result)

EDITED Try something like: 编辑尝试类似:

new_data = []
# Extract all the data and map them by name and status
for value in data:
    name = value.get("name")
    status = value.get("status")
    if name in ['blah', 'blah0', 'blah1', 'blah2', 'blah3']:
         new_data.append(dict(
            name=name,
            status=status))

Option 1 选项1

# loop through the new data
for data in new_data:
    print(data)

# OUTPUT:
{'name': 'blah', 'status': 'ok'}
{'name': 'blah0', 'status': 'ok'}
{'name': 'blah1', 'status': 'ok'}
{'name': 'blah2', 'status': 'ok'}
{'name': 'blah3', 'status': 'ok'}

Option 2 选项2

for data in new_data:
    for key, value in data.items():
        print(key, value)
#OUTPUT:
name blah
status ok
name blah0
status ok
name blah1
status ok
name blah2
status ok
name blah3
status ok

Option 3 选项3

for data in new_data:
        print(data['name'], data['status'])
#OUTPUT
blah ok
blah0 ok
blah1 ok
blah2 ok
blah3 ok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM