从嵌套列表中提取 JSON 值
Extract JSON Value From Nested List
我正在使用 OMDb API 使用 Python 提取 movie/tv 显示数据。我正在尝试从以下 JSON.
获得 IMDB、Rotten Tomatoes 和 Metacritic 评分
{
"title": "One Hundred and One Dalmatians",
"year": "1961",
"rated": "G",
"ratings": [
{
"source": "Internet Movie Database",
"value": "7.2/10"
},
{
"source": "Rotten Tomatoes",
"value": "98%"
},
{
"source": "Metacritic",
"value": "83/100"
}
],
"response": "True"
}
我想要 Rotten Tomatoes 源的嵌套评级列表中的值“98%”。我怎样才能得到它而不是使用 omdb_media['ratings'][1]['Value']
之类的东西?并不总是有烂番茄的条目,我也不能保证顺序,因为可能没有 IMDB 或 Metacritic 的条目,但有烂番茄的条目,所以它的索引发生了变化。
理想情况下,我希望能够搜索 JSON 并通过搜索“Rotten Tomatoes”获得该值。
这可能吗?我该怎么做?
json ={
"title": "One Hundred and One Dalmatians",
"year": "1961",
"rated": "G",
"ratings": [
{
"source": "Internet Movie Database",
"value": "7.2/10"
},
{
"source": "Rotten Tomatoes",
"value": "98%"
},
{
"source": "Metacritic",
"value": "83/100"
}
],
"response": "True"
}
for rating in json["ratings"] :
if(rating["source"] == "Rotten Tomatoes") :
print(rating["value"])
假设 ratings
列表中的每个条目都有一个来源和一个值,并且每个评级都有一个唯一的来源,您可以执行以下操作:
# Generate a new list, with any ratings that aren't from Rotten Tomatoes removed.
rotten_tomatoes_ratings = filter(lambda x: x['source'] == 'Rotten Tomatoes', omdb_media['ratings'])
# Only execute the following code if there exists a rating from Rotten Tomatoes.
if rotten_tomatoes_ratings:
[rating] = rotten_tomatoes_ratings
# Do stuff with rating...
您可以只要求 next()
评级,其中 source
是 "Rotten Tomatoes"
。最后的None
是如果源不匹配的结果,这可以是你想要的任何默认值:
source = 'Rotten Tomatoes'
rt = next((rating['value']
for rating in d['ratings']
if rating['source'] == source), None)
print(rt)
# 98%
我正在使用 OMDb API 使用 Python 提取 movie/tv 显示数据。我正在尝试从以下 JSON.
获得 IMDB、Rotten Tomatoes 和 Metacritic 评分{
"title": "One Hundred and One Dalmatians",
"year": "1961",
"rated": "G",
"ratings": [
{
"source": "Internet Movie Database",
"value": "7.2/10"
},
{
"source": "Rotten Tomatoes",
"value": "98%"
},
{
"source": "Metacritic",
"value": "83/100"
}
],
"response": "True"
}
我想要 Rotten Tomatoes 源的嵌套评级列表中的值“98%”。我怎样才能得到它而不是使用 omdb_media['ratings'][1]['Value']
之类的东西?并不总是有烂番茄的条目,我也不能保证顺序,因为可能没有 IMDB 或 Metacritic 的条目,但有烂番茄的条目,所以它的索引发生了变化。
理想情况下,我希望能够搜索 JSON 并通过搜索“Rotten Tomatoes”获得该值。
这可能吗?我该怎么做?
json ={
"title": "One Hundred and One Dalmatians",
"year": "1961",
"rated": "G",
"ratings": [
{
"source": "Internet Movie Database",
"value": "7.2/10"
},
{
"source": "Rotten Tomatoes",
"value": "98%"
},
{
"source": "Metacritic",
"value": "83/100"
}
],
"response": "True"
}
for rating in json["ratings"] :
if(rating["source"] == "Rotten Tomatoes") :
print(rating["value"])
假设 ratings
列表中的每个条目都有一个来源和一个值,并且每个评级都有一个唯一的来源,您可以执行以下操作:
# Generate a new list, with any ratings that aren't from Rotten Tomatoes removed.
rotten_tomatoes_ratings = filter(lambda x: x['source'] == 'Rotten Tomatoes', omdb_media['ratings'])
# Only execute the following code if there exists a rating from Rotten Tomatoes.
if rotten_tomatoes_ratings:
[rating] = rotten_tomatoes_ratings
# Do stuff with rating...
您可以只要求 next()
评级,其中 source
是 "Rotten Tomatoes"
。最后的None
是如果源不匹配的结果,这可以是你想要的任何默认值:
source = 'Rotten Tomatoes'
rt = next((rating['value']
for rating in d['ratings']
if rating['source'] == source), None)
print(rt)
# 98%