Access a particular field in arbitrarily nested JSON data(访问任意嵌套 JSON 数据中的特定字段)
问题描述
{
"status": "200",
"msg": "",
"data": {
"time": "1515580011",
"video_info": [
{
"announcement": "{"announcement_id":"6","name":"INS\u8d26\u53f7","icon":"http:\/\/liveme.cms.ksmobile.net\/live\/announcement\/2017-08-18_19:44:54\/ins.png","icon_new":"http:\/\/liveme.cms.ksmobile.net\/live\/announcement\/2017-10-20_22:24:38\/4.png","videoid":"15154610218328614178","content":"FOLLOW ME PLEASE","x_coordinate":"0.22","y_coordinate":"0.23"}",
"announcement_shop": "",
如何从这个 json 中获取FOLLOW ME PLEASE"的内容?
How do I grab the content "FOLLOW ME PLEASE" from this json?
replay_data = raw_replay_data['data']['video_info'][0]
announcement = replay_data['announcement']
这用 ['announcement']
抓住一切,我不能这样做 ['announcement']['content']
.
This grab the everything withing ['announcement']
and I can't do ['announcement']['content']
.
这样做的正确方法是什么?
What is the right way to do this?
提前感谢您帮助我解决这个问题.
Thank you in advance for helping me figuring this.
推荐答案
单行 -
>>> json.loads(data['data']['video_info'][0]['announcement'])['content']
'FOLLOW ME PLEASE'
<小时>
为了帮助您了解如何访问数据(这样您就不必再次询问),您需要盯着您的数据.
首先,让我们很好地布置您的数据.您可以使用 json.dumps(data, indent=4)
,也可以使用在线工具,例如 JSONLint.com.
First, let's lay out your data nicely. You can either use json.dumps(data, indent=4)
, or you can use an online tool like JSONLint.com.
{
'data': {
'time': '1515580011',
'video_info': [{
'announcement': ( # ***
"""{
"announcement_id": "6",
"name": "INS\u8d26\u53f7",
"icon": "http:\\/\\/liveme.cms.ksmobile.net\\/live\\/announcement\\/2017-08-18_19:44:54\\/ins.png",
"icon_new": "http:\\/\\/liveme.cms.ksmobile.net\\/live\\/announcement\\/2017-10-20_22:24:38\\/4.png",
"videoid": "15154610218328614178",
"content": "FOLLOW ME PLEASE",
"x_coordinate": "0.22",
"y_coordinate": "0.23"
}"""),
'announcement_shop': ''
}]
},
'msg': '',
'status': '200'
}
*** 请注意,announcement
键中的数据实际上是 more json 数据,我已将其放在单独的行中.
*** Note that the data in the announcement
key is actually more json data, which I've laid out on separate lines.
首先,找出您的数据所在的位置.您正在查找 content
键中的数据,该键由 announcement
键访问,该键是字典列表中的字典的一部分,可以访问通过 video_info
键,该键又由 data
访问.
First, find out where your data resides. You're looking for the data in the content
key, which is accessed by the announcement
key, which is part of a dictionary inside a list of dicts, which can be accessed by the video_info
key, which is in turn accessed by data
.
因此,总而言之,使用以下梯级"下降"作为数据"的阶梯 -
So, in summary, "descend" the ladder that is "data" using the following "rungs" -
数据
,字典video_info
,字典列表announcement
,dicts列表第一个dict中的一个dictcontent
作为 json 数据的一部分.
data
, a dictionaryvideo_info
, a list of dictsannouncement
, a dict in the first dict of the list of dictscontent
residing as part of json data.
<小时>
首先,
i = data['data']
接下来,
j = i['video_info']
接下来,
k = j[0] # since this is a list
如果你只想要第一个元素,这就足够了.否则,您需要迭代:
If you only want the first element, this suffices. Otherwise, you'd need to iterate:
for k in j:
...
接下来,
l = k['announcement']
现在,l
是 JSON 数据.加载它 -
Now, l
is JSON data. Load it -
import json
m = json.loads(l)
最后,
content = m['content']
print(content)
'FOLLOW ME PLEASE'
如果您以后有这种性质的疑问,这应该可以作为指南.
This should hopefully serve as a guide should you have future queries of this nature.
这篇关于访问任意嵌套 JSON 数据中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:访问任意嵌套 JSON 数据中的特定字段
基础教程推荐
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01