简体   繁体   English

如何从JSON字符串中提取N级对象

[英]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.

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