簡體   English   中英

如何在 python 的 json 響應中遍歷所有對象

[英]how can I iterate through all objects in a json response in python

我一直在努力想弄清楚為什么我可以在 json 響應中獲得一些屬性但不能全部獲得(在 Python 中)

例如:運行下面 function 中的代碼時,我在這里找到並成功找到的 3 個屬性(_id、state、類型和名稱)都與其他一些屬性處於同一級別,但似乎我無法獲得其他屬性。 (請參閱 function 下方的完整 json 響應。)

我無法獲得的值為 scan_progress_status、parent_scan_id、parent_scan_type

getScans = scans.get_scans()
scanResults = json.loads(getScans)
results = {}

for x in scanResults['scans']:
    results.update({x['_id'][0:]: {
       'state': x['state'],
       'name': x['name'],
       'type': x['type'],
       'parent_scan_id': x['parent_scan_id']
       
    }
})
y = json.dumps(results)
scanStates = cj.convert_pretty_json(y)

return scanStates

回復

    {
  "scans": [
    {
      "_id": "60c40198152afeca2e5f2cfd",       
      "classification_is_enabled": true,       
      "connectorType": "rdb-postgresql",       
      "created_at": "2021-06-12T00:36:40.322Z",
      "customConnectorType": "default",        
      "ds_connection_name": "client_id_test",  
      "ds_location": null,
      "identityConnectionsUuid": [],
      "isDifferential": false,
      "isSampling": false,
      "name": "GenericScanner Subscan 1/1: client_id_test",
      "number_of_parsing_threads": 2,
      "origin": "client_id_test",
      "parent_scan_id": "60c40198152afeca2e5f2ce7",
      "parent_scan_type": "full_scan",
      "profile_id": null,
      "row_identifier_expression_is_disabled": false,
      "scanSecretKey": "masked0a0VT24aOvlg==",
      "scan_progress_status": {
        "Queued": "2021-06-12T00:36:40.322Z",
        "CollectingMetadata": "2021-06-12T00:36:44.239Z"
      },
      "scanner_group": "default",
      "scanner_type_group": "structured",
      "state": "CollectingMetadata",
      "structured_clustering_enabled": false,
      "type": "sub_scan",
      "updated_at": "2021-06-12T00:36:44.364Z",
      "startTimestamp": 1623458200354,
      "enumerationState": "Completed",
      "scan_enumeration_progress_status": {
        "Started": "2021-06-12T00:36:44.239Z",
        "Completed": "2021-06-12T00:36:44.364Z"
      },
      "scannerId": "4340cbb7-5949-4412-8ddf-afd36dd16d1c",
      "latestCollectionScanned": {
        "fullyQualifiedName": "client_id_test.public.clients",
        "totalRows": null,
        "totalRowsWithFindings": null
      },
      "scannedSize": 0,
      "totalCollections": 0,
      "totalEnumerated": 1,
      "totalFailedCollections": 0,
      "totalFindings": 0,
      "totalRows": 0,
      "totalRowsWithFindings": 0,
      "scan_parts_counter": {
        "New": 1
      }]}

是什么讓 parent_scan_id 和其他一些不同之處足以導致關鍵錯誤?

答案原來是像“parent_scan_id”這樣的屬性,我遇到關鍵錯誤是因為某些塊沒有這個屬性。

解決方案是將變量設置為 parent_scan_id,如果存在則更新我的結果 object

 for x in scanResults['scans']:
    parent_scan_id = x['parent_scan_id'] if 'parent_scan_id' in x else 'undefined'
    results.update({x['_id'][0:]: {
       'state': x['state'],
       'name': x['name'],
       'type': x['type'],
       'parent_scan_id': parent_scan_id
       
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM