读取错误的json,解析示例

时间:2019-01-22 08:10:55

标签: python json parsing

我正在读取某种格式不正确的json。我曾尝试过使用不同的解析器,但都不适合格式的特殊编码吗?

我尝试了几个软件包,但是没有一个能够捕获每个json寄存器中的所有字段

{

"_id" : ObjectId("5c187d6caf0ffa6162eb4f5c"), 

"SDKVersion" : "6.1.7", 

"aaid" : "248c3ab3-919b-4525-b6cc-2c662504235d", 

"appVersion" : "6.2.4", 

"dateTime" : ISODate("2018-12-18T04:32:55.000+0000"), 

"device" : "samsung", 

"id" : NumberInt(5712), 

"installReferrer" : "", 

"isFirstRun" : false, 

"json" : null, 

"language" : "KO", 

"mcc" : NumberInt(450), 

"mnc" : NumberInt(5), 

"model" : "SM-G885S", 

"notificationId" : NumberInt(0), 

"pixelHeight" : NumberInt(2094), 

"pixelWidth" : NumberInt(1080), 

"runningSecs" : NumberInt(0), 

"status" : "pageview", 

"udid" : "6b2be43d1f1fc05c", 

"userDoc" : "", 

"userIdGA" : "", 

"version" : "8.0.0 (26)", 

"wifi" : false, 

"pagename" : "com.mo2o.alsa.modules.passengers.presentation.form.FormPassengersActivity", 

"remoteAddr" : "223.62.215.198", 

"server" : "web26", 

"serverQueueuDateTime1" : "20181218053257", 

"serverQueueuDateTime2" : ISODate("2018-12-18T04:32:57.000+0000"), 

"apiVersion" : "2.0.0", 

"dateTime2" : "2018-12-18T13:32:55+0900"

}

我希望代码能正确识别所有字段并提取格式错误的json,我有超过100万个具有相同格式的寄存器。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我不建议您编写另一个JSON解析器,而是建议您利用Python是一种动态语言的事实,将您拥有的数据转换为有效的Python,并让解释器为您解析。为了证明评论中的建议有效,请尝试

>>> NumberInt = int
>>> ObjectId = str
>>> ISODate = str      # I leave a lambda that calls time.strptime() as an exercise
>>> true = True
>>> false = False
>>> null = None
>>> mydict = eval("""{
"_id" : ObjectId("5c187d6caf0ffa6162eb4f5c"), 
"SDKVersion" : "6.1.7", 
"aaid" : "248c3ab3-919b-4525-b6cc-2c662504235d", 
"appVersion" : "6.2.4", 
"dateTime" : ISODate("2018-12-18T04:32:55.000+0000"), 
"device" : "samsung", 
"id" : NumberInt(5712), 
"installReferrer" : "", 
"isFirstRun" : false, 
"json" : null, 
"language" : "KO", 
"mcc" : NumberInt(450), 
"mnc" : NumberInt(5), 
"model" : "SM-G885S", 
"notificationId" : NumberInt(0), 
"pixelHeight" : NumberInt(2094), 
"pixelWidth" : NumberInt(1080), 
"runningSecs" : NumberInt(0), 
"status" : "pageview", 
"udid" : "6b2be43d1f1fc05c", 
"userDoc" : "", 
"userIdGA" : "", 
"version" : "8.0.0 (26)", 
"wifi" : false, 
"pagename" : "com.mo2o.alsa.modules.passengers.presentation.form.FormPassengersActivity", 
"remoteAddr" : "223.62.215.198", 
"server" : "web26", 
"serverQueueuDateTime1" : "20181218053257", 
"serverQueueuDateTime2" : ISODate("2018-12-18T04:32:57.000+0000"), 
"apiVersion" : "2.0.0", 
"dateTime2" : "2018-12-18T13:32:55+0900"
}""")

eval()调用产生:

>>> mydict
{'userIdGA': '', 'device': 'samsung', 'mnc': 5, 'id': 5712, 'isFirstRun': False, 'serverQueueuDateTime1': '20181218053257', 'appVersion': '6.2.4', 'serverQueueuDateTime2': '2018-12-18T04:32:57.000+0000', 'json': None, 'version': '8.0.0 (26)', 'runningSecs': 0, 'userDoc': '', 'server': 'web26', 'status': 'pageview', 'SDKVersion': '6.1.7', 'mcc': 450, 'dateTime2': '2018-12-18T13:32:55+0900', 'pixelWidth': 1080, 'notificationId': 0, 'pixelHeight': 2094, 'language': 'KO', 'udid': '6b2be43d1f1fc05c', 'wifi': False, 'aaid': '248c3ab3-919b-4525-b6cc-2c662504235d', 'apiVersion': '2.0.0', 'dateTime': '2018-12-18T04:32:55.000+0000', 'installReferrer': '', 'pagename': 'com.mo2o.alsa.modules.passengers.presentation.form.FormPassengersActivity', 'model': 'SM-G885S', '_id': '5c187d6caf0ffa6162eb4f5c', 'remoteAddr': '223.62.215.198'}

并回答有关嵌入式词典的问题:

>>> mydict = eval("""{
"_id" : ObjectId("5c187d6caf0ffa6162eb4f5c"),
"SDKVersion" : "6.1.7",
"aaid" : "248c3ab3-919b-4525-b6cc-2c662504235d",
"appVersion" : "6.2.4",
"dateTime2" : "2018-12-18T13:32:55+0900",
"additional" : { "sell" : "Evento de compra" }
}""")

这是您的示例的故意简化版本,以节省空间。它给出:

>>> mydict
{'SDKVersion': '6.1.7', 'additional': {'sell': 'Evento de compra'}, 'aaid': '248c3ab3-919b-4525-b6cc-2c662504235d', 'dateTime2': '2018-12-18T13:32:55+0900', 'appVersion': '6.2.4', '_id': '5c187d6caf0ffa6162eb4f5c'}