[英]How to get the json files from a value
this is my json files这是我的 json 文件
{
"people1": [
{
"name":"William",
"age": "20",
"location": "Bali"
},
{
"name":"Jose",
"age": "30",
"location": "Canada"
}
]
}
Lets say i want to get the age and location of ''William'' only.可以说我只想获得“威廉”的年龄和位置。 How would I do it?
我该怎么做? for now this is my code but I can't seem to get it.
现在这是我的代码,但我似乎无法理解。
code.py代码.py
import json
with open("names.json") as json_file:
data = json.load(json_file)
for item in data["people1"]:
print(item["name"])
Loop through your data looking for William and then do what you like with his data.遍历你的数据寻找 William,然后用他的数据做你喜欢的事。
for item in data["people1"]:
if item["name"] == "William":
print(item["age"])
print(item["location"])
You can try like this:你可以这样尝试:
import json
j_ = '''{
"people1": [
{
"name":"William",
"age": "20",
"location": "Bali"
},
{
"name":"Jose",
"age": "30",
"location": "Canada"
}
]
}'''
j = json.loads(j_)
for x in j['people1']:
if x['name'] == 'William':
print(x['age'])
print(x['location'])
You do a simple for loop你做一个简单的for循环
for i in data['people1']:
if i['name'] == 'William':
print(i['age'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.