简体   繁体   English

如何导入在 Python 中有多个密钥的 JSON 文件?

[英]How do I import a JSON file which has multiple keys in Python?

For example: I have a program that generates usage logs like this in a JSON file.例如:我有一个程序在 JSON 文件中生成这样的使用日志。 The JSON file log contains a lot of the same key called "activity" like the following: JSON 文件日志包含许多称为“活动”的相同键,如下所示:

  "probe": "PROCESS_PROBE",
  "status": "ProcessCreated",
  "processName": "backgroundTaskHost.exe",
  "path": "C:\\WINDOWS\\system32\\backgroundTaskHost.exe",
  "creationClassName": "Win32_Process",
  "handle": "21632",
  "priority": "Normal",
  "commandLine": "\"C:\\WINDOWS\\system32\\backgroundTaskHost.exe\" -ServerName:CortanaUI.AppXy7vb4pc2dr3kc93kfc509b1d0arkfb2x.mca",
  "handleCount": 236,
  "processId": 21632,
  "parentProcessId": 112,
  "pageFileUsage": 4244,
  "creationDate": "20200410172922.614702+120",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259878750,
    "ticksOfEvent": 637221365629757593
  }
},
"activity":{
  "probe": "PROCESS_PROBE",
  "status": "ProcessDeleted",
  "processName": "RuntimeBroker.exe",
  "path": "C:\\Windows\\System32\\RuntimeBroker.exe",
  "creationClassName": "Win32_Process",
  "handle": "8504",
  "priority": "Normal",
  "handleCount": 285,
  "processId": 8504,
  "parentProcessId": 112,
  "pageFileUsage": 3180,
  "creationDate": "20200410172757.934567+120",
  "terminationDate": null,
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259883953,
    "ticksOfEvent": 637221365681937472
  }
},
"activity":{
  "probe": "FILERESOURCE_PROBE",
  "status": "Changed",
  "path": "C:\\Users\\datta\\eclipse\\jee-2019-12",
  "entityName": "eclipse",
  "extension": "",
  "attributes": "Directory",
  "owner": "null",
  "length": 0,
  "isReadOnly": false,
  "creationTime": "2020-01-17T09:42:08.5092897+01:00",
  "lastWriteTime": "2020-03-25T10:56:10.7382329+01:00",
  "lastAccessTime": "2020-04-10T17:29:29.9811767+02:00",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259885750,
    "ticksOfEvent": 637221365699837331
  }
},
"activity":{
  "probe": "FILERESOURCE_PROBE",
  "status": "Changed",
  "path": "C:\\Users\\datta\\eclipse",
  "entityName": "jee-2019-12",
  "extension": "",
  "attributes": "Directory",
  "owner": "null",
  "length": 0,
  "isReadOnly": false,
  "creationTime": "2020-01-17T09:42:08.5083+01:00",
  "lastWriteTime": "2020-01-17T09:42:08.5092897+01:00",
  "lastAccessTime": "2020-04-10T17:29:29.9801436+02:00",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259885750,
    "ticksOfEvent": 637221365699906960
  }
},
"activity":{
  "probe": "FILERESOURCE_PROBE",
  "status": "Changed",
  "path": "C:\\Users\\datta",
  "entityName": "eclipse",
  "extension": "",
  "attributes": "Directory",
  "owner": "null",
  "length": 0,
  "isReadOnly": false,
  "creationTime": "2020-01-17T09:42:08.5083+01:00",
  "lastWriteTime": "2020-01-17T09:42:08.5083+01:00",
  "lastAccessTime": "2020-04-10T17:29:29.9922013+02:00",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259885765,
    "ticksOfEvent": 637221365699922013
  }
}
}

I would like to load them into a python program.我想将它们加载到 python 程序中。 Currently, I am using logData = json.load(logfile) to load it but the problem is when I do so, it returns me a python dict with only the last "activity" key and the rest of the "activity" keys get over-written.目前,我正在使用logData = json.load(logfile)来加载它,但问题是当我这样做时,它返回给我一个 python 字典,只有最后一个“活动”键和 Z65E8800B5C6800AAD896F888B2A6 的“活动”键-书面。 I don't know how to load all of them.我不知道如何加载所有这些。 I would appreciate if you guys can help me with that.如果你们能帮助我,我将不胜感激。 Thank you.谢谢你。

Having an object using different content for the same key looks odd, it'd probably make more sense to get an array of object in that case.让一个 object 对同一个键使用不同的内容看起来很奇怪,在这种情况下获得一个 object 数组可能更有意义。 But assuming you cannot control that, you must do "something else" with the incoming data and not not unpack key/value pairs into mapping objects.但是假设您无法控制它,您必须对传入数据执行“其他操作”,而不是将键/值对解压缩到映射对象中。 You can tell json.load() how to treat these pairs by registering object_pairs_hook to which the pair is passed and what it returns is becomes the corresponding python object.您可以告诉json.load()如何处理这些对,方法是注册传递对的object_pairs_hook并返回相应的 python object。 For instance:例如:

logdata = json.load(logfile, object_pairs_hook=tuple)

And instead of a dict s (which would have been the default) you end up with tuple s of (key, value) .而不是一个dict s(这将是默认的)你最终得到tuple s of (key, value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM