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

时间:2022-10-26 16:02:58

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

我想从以下JSON字符串中提取wind对象的数据:

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

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'

2 个解决方案

#1


1  

I want to extract wind object's data

我想提取风对象的数据

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”}

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

附:由于这是一个单独的对象,我不确定你想通过将它变成pandas.DataFrame来实现什么

#2


1  

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中获取条件对象。一旦有了条件对象,就可以拉出风对象。

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

Metar没有风儿,所以metar ['wind']无法访问任何东西。 metar ['conditions']会起作用,因为有条件的孩子。

Hope that helps!

希望有所帮助!

#1


1  

I want to extract wind object's data

我想提取风对象的数据

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”}

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

附:由于这是一个单独的对象,我不确定你想通过将它变成pandas.DataFrame来实现什么

#2


1  

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中获取条件对象。一旦有了条件对象,就可以拉出风对象。

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

Metar没有风儿,所以metar ['wind']无法访问任何东西。 metar ['conditions']会起作用,因为有条件的孩子。

Hope that helps!

希望有所帮助!