访问任意嵌套 JSON 数据中的特定字段 [英] Access a particular field in arbitrarily nested JSON data

查看:32
本文介绍了访问任意嵌套 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" -

  1. 数据,字典
  2. video_info,字典列表
  3. announcement,dicts列表第一个dict中的一个dict
  4. content 作为 json 数据的一部分.
  1. data, a dictionary
  2. video_info, a list of dicts
  3. announcement, a dict in the first dict of the list of dicts
  4. content 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 数据中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆