英文:
Printing data in python by parsing json
问题
我正在尝试获取正确的表示法以解析JSON数据并在Python中打印所需的输出
这是一个示例JSON文件
播放信息JSON响应:
```json
{
"id": 111,
"age": 22,
"Hobby": [
{
"List": 1,
"Sev": "medium",
"name": "play 1",
"game": "tennis"
},
{
"List": 2,
"Sev": "high",
"name": "play 2",
"game": "football"
},
{
"List": 2,
"Sev": "high",
"name": "play 3",
"game": "racing"
}
]
}
我正在寻找在Python中打印play 2的结果(即来自JSON文件的football)的命令。感谢帮助。
<details>
<summary>英文:</summary>
I am trying to get the correct notation to parse json data and print the required output in python
here is a sample json file
Play info Json Response:
{
"id": 111,
"age": 22,
"Hobby": [
{
"List": 1,
"Sev": "medium",
"name": "play 1",
"game": "tennis",
},
{
"List": 2,
"Sev": "high",
"name": "play 2",
"game": "football",
},
{
"List": 2,
"Sev": "high",
"name": "play 3",
"game": "racing",
}
]
I am looking for python commands to print the outcome of play 2 ie, football from the json file..Appreciate the help.
</details>
# 答案1
**得分**: 0
a = [hobby['game'] for hobby in json['Hobby'] if hobby['name'] == 'play 2']
print(a)
Output:
['football']
<details>
<summary>英文:</summary>
Maybe this can help you:
json = { "id": 111, "age": 22, "Hobby": [
{ "List": 1, "Sev": "medium", "name": "play 1", "game": "tennis", },
{ "List": 2, "Sev": "high", "name": "play 2", "game": "football", },
{ "List": 2, "Sev": "high", "name": "play 3", "game": "racing", } ]}
a = [hobby['game'] for hobby in json['Hobby'] if hobby['name'] == 'play 2']
print(a)
Output:
['football']
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论