Python—将JSON响应的一部分存储为逗号分隔的字符串

Python—将JSON响应的一部分存储为逗号分隔的字符串,python,json,database,python-2.7,Python,Json,Database,Python 2.7,我有一个JSON响应字符串,其中包含来自多个防病毒站点的扫描结果。它看起来像- "scans": { "Bkav": { "detected": false, "version": "1.3.0.9466", "result": null, "update": "20180609" }, "MicroWorld-eScan": { "detected": true, "version": "14.0.297.0", "resul

我有一个JSON响应字符串,其中包含来自多个防病毒站点的扫描结果。它看起来像-

"scans": {
  "Bkav": {
    "detected": false,
    "version": "1.3.0.9466",
    "result": null,
    "update": "20180609"
  },
  "MicroWorld-eScan": {
    "detected": true,
    "version": "14.0.297.0",
    "result": "W97M.Downloader.AIU",
    "update": "20180611"
  },
  "CMC": {
    "detected": false,
    "version": "1.1.0.977",
    "result": null,
    "update": "20180610"
  },
  "CAT-Quickheal": {
    "detected": true,
    "version": "14.00",
    "result": "X97M.Dropper.PD",
    "update": "20180611"
  },
  "McAfee": {
    "detected": true,
    "version": "6.0.6.653",
    "result": "X97M/Downloader.asi",
    "update": "20180611"
  },
},
"tags": {
...
},
如何从JSON中的
scans
标记中提取所有非空结果(所有非空结果),并将其存储到数据库表列中的单个逗号分隔字符串中


谢谢

可能有一种更有效的方法,但我认为这应该为您提供所需的逗号分隔字符串值:

import json

scans = response_data.get('scans', [])
scan_results = (scans[key]['result'] for key in scans.keys())
csv = ', '.join((result for result in scan_results if result is not None))

可能有一种更有效的方法,但我认为这应该为您提供所需的逗号分隔字符串值:

import json

scans = response_data.get('scans', [])
scan_results = (scans[key]['result'] for key in scans.keys())
csv = ', '.join((result for result in scan_results if result is not None))

这真的是JSON吗? 我知道,JSON不能用分号分隔。 它应该像下面这样

"scans": {
  "Bkav": {
    "detected": false,
    "version": "1.3.0.9466",
    "result": null,
    "update": "20180609"
  },
  "MicroWorld-eScan": {
    "detected": true,
    "version": "14.0.297.0",
    "result": "W97M.Downloader.AIU",
    "update": "20180611"
  },
  "CMC": {
    "detected": false,
    "version": "1.1.0.977",
    "result": null,
    "update": "20180610"
  },
  "CAT-Quickheal": {
    "detected": true,
    "version": "14.00",
    "result": "X97M.Dropper.PD",
    "update": "20180611"
  },
  "McAfee": {
    "detected": true,
    "version": "6.0.6.653",
    "result": "X97M/Downloader.asi",
    "update": "20180611"
  },
},
"tags": {
...
},
import json

jsonDict = json.loads(yourJSON)

results = []
for scanElement in jsonDict['scans']:
    if scanElement['result'] != None:
        results.append(scanElement['result'])

##save the results to your DB
将JSON加载到PythonDict后,可以执行如下操作

"scans": {
  "Bkav": {
    "detected": false,
    "version": "1.3.0.9466",
    "result": null,
    "update": "20180609"
  },
  "MicroWorld-eScan": {
    "detected": true,
    "version": "14.0.297.0",
    "result": "W97M.Downloader.AIU",
    "update": "20180611"
  },
  "CMC": {
    "detected": false,
    "version": "1.1.0.977",
    "result": null,
    "update": "20180610"
  },
  "CAT-Quickheal": {
    "detected": true,
    "version": "14.00",
    "result": "X97M.Dropper.PD",
    "update": "20180611"
  },
  "McAfee": {
    "detected": true,
    "version": "6.0.6.653",
    "result": "X97M/Downloader.asi",
    "update": "20180611"
  },
},
"tags": {
...
},
import json

jsonDict = json.loads(yourJSON)

results = []
for scanElement in jsonDict['scans']:
    if scanElement['result'] != None:
        results.append(scanElement['result'])

##save the results to your DB

这真的是JSON吗? 我知道,JSON不能用分号分隔。 它应该像下面这样

"scans": {
  "Bkav": {
    "detected": false,
    "version": "1.3.0.9466",
    "result": null,
    "update": "20180609"
  },
  "MicroWorld-eScan": {
    "detected": true,
    "version": "14.0.297.0",
    "result": "W97M.Downloader.AIU",
    "update": "20180611"
  },
  "CMC": {
    "detected": false,
    "version": "1.1.0.977",
    "result": null,
    "update": "20180610"
  },
  "CAT-Quickheal": {
    "detected": true,
    "version": "14.00",
    "result": "X97M.Dropper.PD",
    "update": "20180611"
  },
  "McAfee": {
    "detected": true,
    "version": "6.0.6.653",
    "result": "X97M/Downloader.asi",
    "update": "20180611"
  },
},
"tags": {
...
},
import json

jsonDict = json.loads(yourJSON)

results = []
for scanElement in jsonDict['scans']:
    if scanElement['result'] != None:
        results.append(scanElement['result'])

##save the results to your DB
将JSON加载到PythonDict后,可以执行如下操作

"scans": {
  "Bkav": {
    "detected": false,
    "version": "1.3.0.9466",
    "result": null,
    "update": "20180609"
  },
  "MicroWorld-eScan": {
    "detected": true,
    "version": "14.0.297.0",
    "result": "W97M.Downloader.AIU",
    "update": "20180611"
  },
  "CMC": {
    "detected": false,
    "version": "1.1.0.977",
    "result": null,
    "update": "20180610"
  },
  "CAT-Quickheal": {
    "detected": true,
    "version": "14.00",
    "result": "X97M.Dropper.PD",
    "update": "20180611"
  },
  "McAfee": {
    "detected": true,
    "version": "6.0.6.653",
    "result": "X97M/Downloader.asi",
    "update": "20180611"
  },
},
"tags": {
...
},
import json

jsonDict = json.loads(yourJSON)

results = []
for scanElement in jsonDict['scans']:
    if scanElement['result'] != None:
        results.append(scanElement['result'])

##save the results to your DB

你已经说过你应该做什么了,你写了什么代码并遇到了问题吗?@ARMAN我不知道如何准确地遍历JSON字符串并在扫描中循环以仅获取NOTNULL值。也许检查每个字段,如果它不是
null
将其保存在新的JSON中?是否需要这样做-
results=[]
对于r中的扫描['scans']:
如果扫描['result']!='null':
results.append(scan['result'])
一旦将JSON加载到Python dict中,您将在scan中使用
'result'检查dict中的字段是否为null。
scan['result']!='null'
可能不起作用,除非null硬编码到原始JSON中的字符串文本“null”中。您已经说过应该做什么,您是否编写了任何代码并遇到了问题?@ARMAN我不知道如何准确地遍历JSON字符串并在扫描中循环以仅获取非空值。也许可以检查每个字段,如果它不是
null
则将其保存在新的JSON中?是否需要这样做-
results=[]
用于r['scans'中的扫描:
如果扫描['result']!='null':
results.append(scan['result'])
一旦将JSON加载到Python dict中,您将在scan
中使用
'result'检查dict中的字段是否为null。
scan['result']!='null“
可能不起作用,除非null硬编码到原始JSON中的字符串文本“null”。或者
,'.join(filter(None,scan_results))
。或者
,'.join(filter(None,scan_results))
。抱歉,是的,这应该是一个逗号。谢谢我要试试这个!对不起,是的,那应该是个逗号。谢谢我要试试这个!