如何解析这个JSON数据到Python列表?

时间:2016-06-16 21:46:42

标签: python json

我有一个json文件,如下所示:

[{"lat":51.877743,"lng":-0.4116338,"acc":0,"time":0},`{"lat":51.877743,"lng":-0.4116338,"acc":20,"time":1465386382293},{"lat":51.877743,"lng":-0.4116338,"acc":20,"time":1465386412347}, ...`

我使用以下代码阅读它们:

import json

with open('data.json') as data_file:    
    data = json.load(data_file)

,数据如下:

data
Out[18]: 
[{u'acc': 0, u'lat': 51.877743, u'lng': -0.4116338, u'time': 0},
 {u'acc': 20, u'lat': 51.877743, u'lng': -0.4116338, u'time': 1465386382293L},
 {u'acc': 20, u'lat': 51.877743, u'lng': -0.4116338, u'time': 1465386412347L},

我想将'lat'和'lng'字段提取到这样的列表中:

array([[ 0.37291534,  0.90496579],
       [ 0.43889613,  0.62523318],
       [ 0.96554937,  0.73811836],
       [ 0.9254325 ,  0.51556322],
       [ 0.26246525,  0.01470611],
       [ 0.73168115,  0.99624888],
       [ 0.38049958,  0.28766334],
       [ 0.94917181,  0.60546656],
       [ 0.52672308,  0.60608954],
       [ 0.03778316,  0.92360363]])

我该怎么做?

3 个答案:

答案 0 :(得分:2)

pandas方法:

import pandas as pd

In [109]: df = pd.DataFrame(data)

In [110]: df
Out[110]:
   acc        lat       lng           time
0    0  51.877743 -0.411634              0
1   20  51.877743 -0.411634  1465386382293
2   20  51.877743 -0.411634  1465386412347

In [111]: df[['lat','lng']]
Out[111]:
         lat       lng
0  51.877743 -0.411634
1  51.877743 -0.411634
2  51.877743 -0.411634

In [112]: df[['lat','lng']].values
Out[112]:
array([[ 51.877743 ,  -0.4116338],
       [ 51.877743 ,  -0.4116338],
       [ 51.877743 ,  -0.4116338]])

答案 1 :(得分:1)

您可以尝试以下几点:

what_you_want = [[e['lat'], e['lng']] for e in data]

这可以进一步转换为数组,或者更好地转换为numpy.array

我不禁要建议您查看pandas.DataFrame

答案 2 :(得分:1)

你可以简单地使用列表理解:

>>> [[i['lat'], i['lng']] for i in data]
[[51.877743, -0.4116338], [51.877743, -0.4116338], [51.877743, -0.4116338]]

要获得风格化阵列,请使用numpy

>>> import numpy as np
>>> np.array([[i['lat'], i['lng']] for i in data])
array([[ 51.877743 ,  -0.4116338],
       [ 51.877743 ,  -0.4116338],
       [ 51.877743 ,  -0.4116338]])