需要帮助格式化/解析此列表

时间:2017-11-16 19:19:41

标签: python json tuples

我不是开发人员,如果这是一个愚蠢的问题,或者我的术语不正确,那就很抱歉。我正在编写一个脚本来调用CMDB的API,但我不知道如何处理从它发回的数据。它似乎是一个列表类型,但我无法通过键名引用任何内容。有没有办法将它转换为我可以轻松操作并从中提取数据的东西?

这是我的代码:

import requests
import json

r=requests.post('API.URL', data={'grant_type': 'password', 'client_id':'#######', 'username': 'user', 'password': 'password'})
json_data = json.loads(r.content)
token = json_data['access_token']
data ={
  "filters": [
    {
      "fieldId": "937905400191ae67dd03ab4b79968fcbaa264b1a75",
      "operator": "eq",
      "value": "hostname"
    }
  ],
  "fields":[
    '9426b6ddf3cb971488517145e39efc5aa7f16fec46',
    '9343f8800b3917f26533954918a6388ae8c863507f',
    '9379053db492ece14816704ef5a9e3e567e217511b',
    '9343f93fc4c8422bcf24e74a9a86035bb7d0248b00',
    '941ba290776d6f51ce35664246927b958330a753b2'
  ],
  "association": "Configuration Item",
  "busObId": "93dada9f640056ce1dc67b4d4bb801f69104894dc8",
  "includeAllFields": 'false',
  "pageNumber": 0,
  "pageSize": 300,
  "scope": "Global",
  "scopeOwner": "(None)",
  "searchName": "APItest"
}
payload = json.dumps(data)
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization':'bearer '+token}
search=requests.post('http://API.URL', headers=headers, data=payload)
search_json = json.loads(search.content)
bo = search_json['businessObjects']
print(bo)

以下是回复:

[
    {
        "busObRecId": "9423ad7d617390fdc956ee4302a69d0ccf1a37a4c1",
        "hasError": false,
        "links": [
            {
                "url": "http://URL",
                "name": "Delete Record"
            }
        ],
        "fields": [
            {
                "displayName": "Business Sponsor",
                "name": "Business Sponsor",
                "value": "",
                "html": null,
                "dirty": false,
                "fieldId": "9426b6ddf3cb971488517145e39efc5aa7f16fec46"
            },
            {
                "displayName": "Owned By",
                "name": "Owned By",
                "value": "John Doe",
                "html": null,
                "dirty": false,
                "fieldId": "9343f8800b3917f26533954918a6388ae8c863507f"
            },
            {
                "displayName": "Asset Status",
                "name": "Asset Status",
                "value": "Active",
                "html": null,
                "dirty": false,
                "fieldId": "9379053db492ece14816704ef5a9e3e567e217511b"
            },
            {
                "displayName": "Description",
                "name": "Description",
                "value": "Automation Server",
                "html": null,
                "dirty": false,
                "fieldId": "9343f93fc4c8422bcf24e74a9a86035bb7d0248b00"
            },
            {
                "displayName": "Data Center Location",
                "name": "Data Center Location",
                "value": "",
                "html": null,
                "dirty": false,
                "fieldId": "941ba290776d6f51ce35664246927b958330a753b2"
            }
        ],
        "errorMessage": null,
        "busObPublicId": "9423ad7d617390fdc956ee4302a69d0ccf1a37a4c1",
        "busObId": "93dada9f640056ce1dc67b4d4bb801f69104894dc8",
        "errorCode": null
    }
]

type()将对象bo显示为列表,len()表示它只有一个元素,因此我不确定如何在不破坏数据的情况下从中提取数据它剥离了人物。

1 个答案:

答案 0 :(得分:0)

您无法通过键名引用任何内容的原因是您的输出是一个列表。一个只包含一个字典元素的列表。如果你打印出来

bo[0]

你得到的是整个数据,没有[和]符号。至于字典,我们现在可以通过键访问不同的元素,例如:

print(bo["busObId"])

将返回以下值:

93dada9f640056ce1dc67b4d4bb801f69104894dc

假设你想要打印出“fields”的第一个元素的fieldId。您可以通过以下方式执行此操作:

print(bo[0]["fields"][0]["fieldId"])

希望这有帮助。