2015-06-20 136 views
1

我有一個JSON文件格式如下在python中解析嵌套的JSON?

{ 
    "_id": { 
    "$oid": "5458c00ceb23024b941be4bb" 
    }, 
    "gpstime": 0.046575, 
    "gpslat": 12.94492917, 
    "readingtime": { 
    "$date": "2014-11-04T17:28:10.000+0000" 
    }, 
    "gpslong": 77.56115458, 
    "deviceid": "11119828", 
    "time": "Tue Nov 4 12:01:16 2014", 
    "location": [ 
    12.94492917, 
    77.56115458 
    ] 
}                           

我用下面的代碼來分析它,但它是直到上日期錯過了

import json 
import csv 
import pandas as pa 

with open('readings.json', 'rb') as f: 
    data = f.readlines() 

data = map(lambda x: x.rstrip(), data) 
data_json_str = "[" + ','.join(data) + "]" 
data_df = pa.read_json(data_json_str) 

我得到readingtime列如下

readingtime {u'$date': u'2014-11-04T17:27:50.000+0000'}                 

但是在閱讀時錯過了%date如何解決這個問題?

回答

0

time場被正確讀取,它正確地獲得自己的列:

>>> print data_df.time 
0 Tue Nov 4 12:01:16 2014 
Name: time, dtype: object 

請注意,無論是$date也不time字段intepreted,他們只是加載字符串。

+1

如何在解析時將時間字段轉換爲日期時間格式?另外如何正確解析閱讀時間字段以獲取日期和時間? – Nishad

+0

http://stackoverflow.com/questions/17134716/convert-dataframe-column-type-from-string-to-datetime – dlask