python - 使用 Python 解析复杂的 JSON

标签 python json parsing

我对 JSON 和 Python 很陌生,并尝试使用通过 GET 请求获得的复杂 JSON 输出。这是 JSON 输出的一个示例(这是一小部分,但原理是相同的):

{
  "innerSet": [
    {
      "clusterUID": {
        "id": 3585057579401361143
      },
      "rpasState": [
        {
          "rpaUID": {
            "clusterUID": {
              "id": 3585057579401361143
            },
            "rpaNumber": 1
          },
          "status": "OK",
          "repositoryConnectivityStatus": {
            "accessStatus": "OK",
            "multipathingProblems": false
          },
          "remoteRPAsDataLinkStatus": [
            {
              "JsonSubType": "RPAConnectivityStatus",
              "clusterUID": {
                "id": 2671811049708195677
              },
              "entityType": "RPA",
              "connectivityStatus": "OK",
              "rpaUID": {
                "clusterUID": {
                  "id": 2671811049708195677
                },
                "rpaNumber": 1
              }
            }
          ],
         }
      ]
    }
 ]
}

我试图找到打印单个值的最佳方法。例如,我需要“connectivityStatus”的值。任何帮助将不胜感激。

我能够解析简单的 JSON 输出。我已经成功获得了整个innerSet树:

x = requests.get('website.com)
d = x.json() print (d['innerSet']) 

但是,我无法按下较低的键。例如,获取“clusterUID”中“id”键的值:

print (d['innerSet']['clusterUID']['id']) 

导致以下错误:TypeError:列表索引必须是整数,而不是 str

问候, 亚基尔。

最佳答案

你可以这样做:

import simplejson as json
data = json.loads(s)
print data['innerSet'][0]['rpasState'][0]['remoteRPAsDataLinkStatus'][0]['connectivityStatus']

关于python - 使用 Python 解析复杂的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32311824/

相关文章:

jquery - Internet Explorer 9 给出 parseJSON 错误

javascript - 如何将 json POST 数据作为对象传递给 Web API 方法?

Scala 解析器组合器 : getting a stackoverflow with packratparsers

java - Jericho-html : is it possible to extract text with reference to positions in source file?

Python。相同的字符串以不同的编码出现?

python - 使用导数确定二维数组中局部最大值的坐标

java - JPy 与 Java 10

python - 将数据帧行聚合到字典中

java - 使用 Jackson 的不可变类反序列化 JSON 平面对象

parsing - Antlr 空格标记错误