Access to a specific field in arbitrary nested JSON data

{
  "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 can I grab the contents of "FOLLOW ME PLEASE" from this json?

replay_data = raw_replay_data['data']['video_info'][0]
announcement = replay_data['announcement']

It will capture all that s ['announcement'], and I can’t do it ['announcement']['content'].

What is the right way to do this?

Thank you in advance for your help in this.

+4
source share
3 answers

In one 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 need to ask again), you need to look at your data.

First, let it lay out your data beautifully. You can 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 json-, .

, . content, announcement, dicts, video_info, data.

, , "" , "", "" -

  • data,
  • video_info, dicts
  • announcement, dict dicts
  • content, json.

-,

i = data['data']

j = i['video_info']

k = j[0] # since this is a list

, . :

for k in j:
    ...

,

l = k['announcement']

l - JSON. -

import json
m = json.loads(l)

,

content = m['content']

print(content)
'FOLLOW ME PLEASE'

, , , .

+6

JSON; , 'annoucement', , , JSON.

:

import json

replay_data = raw_replay_data['data']['video_info'][0]
announcement = json.loads(replay_(data['announcement'])
print(announcement['content'])

.

+2

"announcement" - JSON. , , .

0
source

Source: https://habr.com/ru/post/1692001/


All Articles