Accessing Nested Data in Complex JSON
How do we access the "content" field from the following 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": "" } ] } }
Solution
To extract the desired "content" value, we must first load the JSON data into a Python dict. Then, we traverse the nested data structure as follows:
Python code:
import json raw_data = { # JSON data pasted here } data = raw_data['data']['video_info'][0] # Convert the announcement string to a dict announcement_data = json.loads(data['announcement']) # Retrieve the desired content content = announcement_data['content'] print(content) # Output: 'FOLLOW ME PLEASE'
By following this approach, we can navigate complex JSON structures and extract the desired data efficiently.
The above is the detailed content of How to Access Nested JSON Data: Extracting the \'content\' Field?. For more information, please follow other related articles on the PHP Chinese website!