简体   繁体   中英

How to extract N-level object from JSON string

I want to extract wind object's data from the following JSON string:

在此处输入图片说明

import pandas as pd
from urllib2 import Request, urlopen
import json
from pandas.io.json import json_normalize

request=Request('...')
response_weather = urlopen(request)
w = response_weather.read()
metar = json.loads(w)
wind = pd.DataFrame(json_normalize(metar['wind']))
print wind


KeyError: 'wind'

To get to the wind object, you first have to get the conditions object out of metar . Once you have the conditions object, you can then pull out the wind object.

Metar doesn't have a wind child, so metar['wind'] doesn't access anything. metar['conditions'] will work, because there is a conditions child .

Hope that helps!

I want to extract wind object's data

Then you want you want metar['conditions']['wind']

And based on your screenshot, that key contains the following object: {direction: 60, directionIsVariable: false, speedKnots: "3.00"}

PS Since that's a single object, I'm not sure what you want to achieve by making it into a pandas.DataFrame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM