我有一个大小为109 Mgb的有效JSON文件,其格式如下。
[{"sensorType":"Radarcape","sensorLatitude":{"double":51.758894},"sensorLongitude":{"double":-1.256654},"sensorAltitude":{"double":200.0},"timeAtServer":1.429617600000027E9,"timeAtSensor":null,"timestamp":{"double":9.68628281E8},"rawMessage":"8d4008f999059a90c09719d7b430","sensorSerialNumber":13020235,"RSSIPacket":{"double":52.0},"RSSIPreamble":null,"SNR":null,"confidence":null}
,{"sensorType":"Radarcape","sensorLatitude":{"double":51.758894},"sensorLongitude":{"double":-1.256654},"sensorAltitude":{"double":200.0},"timeAtServer":1.429617600001544E9,"timeAtSensor":null,"timestamp":{"double":9.74843812E8},"rawMessage":"8f406a9a5913d2d51fbe8ab14fdf","sensorSerialNumber":13020235,"RSSIPacket":{"double":72.0},"RSSIPreamble":null,"SNR":null,"confidence":null}]
读取JSON文件的简单代码:
import json
with open ("jsonfile.txt",'r') as f:
j=json.load(f)
我正在尝试解析它以将其转换为SQLite格式。但是,每次我收到以下消息错误。
Traceback (most recent call last):
File "C:/Users/PycharmProjects/jsontosqlite/test.py", line 5, in <module>
a=json.loads("newfile.json")
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
答案 0 :(得分:1)
如果我将问题中显示的JSON数据(确切地)放入jsonfile.txt
文件并运行它,以下内容适用于我:
import json
with open ("jsonfile.txt", 'r') as f:
j = json.loads(f.read())
print(json.dumps(j, indent=4))
输出:
[
{
"sensorType": "Radarcape",
"sensorLatitude": {
"double": 51.758894
},
"sensorLongitude": {
"double": -1.256654
},
"sensorAltitude": {
"double": 200.0
},
"timeAtServer": 1429617600.000027,
"timeAtSensor": null,
"timestamp": {
"double": 968628281.0
},
"rawMessage": "8d4008f999059a90c09719d7b430",
"sensorSerialNumber": 13020235,
"RSSIPacket": {
"double": 52.0
},
"RSSIPreamble": null,
"SNR": null,
"confidence": null
},
{
"sensorType": "Radarcape",
"sensorLatitude": {
"double": 51.758894
},
"sensorLongitude": {
"double": -1.256654
},
"sensorAltitude": {
"double": 200.0
},
"timeAtServer": 1429617600.001544,
"timeAtSensor": null,
"timestamp": {
"double": 974843812.0
},
"rawMessage": "8f406a9a5913d2d51fbe8ab14fdf",
"sensorSerialNumber": 13020235,
"RSSIPacket": {
"double": 72.0
},
"RSSIPreamble": null,
"SNR": null,
"confidence": null
}
]