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

查看:107
本文介绍了访问任意嵌套的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获取内容请关注我"?

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'].

正确的方法是什么?

在此先感谢您帮助我解决这个问题.

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. data,字典
  2. video_info,字典列表
  3. announcement,字典列表中第一个字典中的一个字典
  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.


首先,


First,

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天全站免登陆