[英]How to extract N-level object from JSON string
I want to extract wind
object's data from the following JSON string: 我想从以下JSON字符串中提取
wind
对象的数据:
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
. 要获得
wind
对象,首先必须从metar
获取conditions
对象。 Once you have the conditions
object, you can then pull out the wind
object. 一旦有了
conditions
对象,就可以拉出wind
对象。
Metar
doesn't have a wind
child, so metar['wind']
doesn't access anything. Metar
没有wind
的孩子,所以metar['wind']
不访问任何。 metar['conditions']
will work, because there is a conditions child
. metar['conditions']
将起作用,因为有一个conditions child
。
Hope that helps! 希望有帮助!
I want to extract
wind
object's data我想提取
wind
对象的数据
Then you want you want metar['conditions']['wind']
然后,您想使用
metar['conditions']['wind']
And based on your screenshot, that key contains the following object: {direction: 60, directionIsVariable: false, speedKnots: "3.00"}
根据您的屏幕截图,该键包含以下对象:
{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
附注:由于这是单个对象,因此我不确定将其变成
pandas.DataFrame
想要实现什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.