首页 > 解决方案 > 读取不正确的json,解析示例

问题描述

我正在阅读一种格式不正确的 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 万个具有相同格式的寄存器。有任何想法吗?

标签: pythonjsonparsing

解决方案


我不建议您编写另一个 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'}

推荐阅读