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

查看:36
本文介绍了访问任意嵌套的 JSON 数据中的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<代码>{"状态": "200","msg": "",数据": {"时间": "1515580011",视频信息":[{"公告": "{"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"的内容?

replay_data = raw_replay_data['data']['video_info'][0]公告 = replay_data['公告']

这用 ['announcement'] 获取所有内容,而我做不到 ['announcement']['content'].

这样做的正确方法是什么?

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

解决方案

在一行中 -

<预><代码>>>>json.loads(data['data']['video_info'][0]['announcement'])['content']'请跟我走'

<小时>

为了帮助您了解如何访问数据(这样您就不必再问了),您需要盯着您的数据.

首先,让我们很好地布置您的数据.您可以使用 json.dumps(data, indent=4),也可以使用像 JSONLint 这样的在线工具.com.

<代码>{'数据': {'时间':'1515580011','视频信息':[{'公告': (    # ***"""{"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": "请跟我来","x_coordinate": "0.22",y_坐标":0.23"}"""),'公告_商店':''}]},'味精':'','状态':'200'}

*** 请注意,announcement 键中的数据实际上是more json 数据,我已将其放在单独的行中.

首先,找出您的数据所在的位置.您正在查找 content 键中的数据,该键可通过 announcement 键访问,该键是 dicts 列表中字典的一部分,可以访问通过 video_info 键,该键依次由 data 访问.

所以,总而言之,使用以下梯级"下降"作为数据"的阶梯 -

  1. data,一个字典
  2. video_info,字典列表
  3. announcement,dicts列表第一个dict中的一个dict
  4. content 作为 json 数据的一部分.

<小时>

首先

i = data['data']

接下来,

j = i['video_info']

接下来,

k = j[0] # 因为这是一个列表

如果你只想要第一个元素,这就足够了.否则,您需要迭代:

for k in j:...

接下来,

l = k['公告']

现在,l 是 JSON 数据.加载它 -

导入jsonm = json.loads(l)

最后,

content = m['content']

print(content)'请跟我走'

如果您将来有这种性质的查询,这应该可以作为指南.

{
  "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": "",

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

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.

解决方案

In a single line -

>>> json.loads(data['data']['video_info'][0]['announcement'])['content']
'FOLLOW ME PLEASE'


To help you understand how to access data (so you don't have to ask again), you'll need to stare at your data.

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'
} 

*** Note that the data in the announcement key is actually more json data, which I've laid out on separate lines.

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

Next,

j = i['video_info']

Next,

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:
    ...

Next,

l = k['announcement']

Now, l is JSON data. Load it -

import json
m = json.loads(l)

Lastly,

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