在Python中处理JSON

时间:2017-03-06 22:21:09

标签: python json

我有一个JSON响应:

{
  "SiteInfo": [
    {
      "'LOB03023D'": {
        "errorText": "Success",
        "status": "1",
        "parameterList": {
          "aloha_mask": "255.255.255.0",
          "beta_mask": "255.255.255.224",
          "omega_mask": "0",
        }
      }
    }
  ],
  "Error Text": null,
  "API RequestID": "11",
  "Status": 1,
  "Username": "NMS@internal.com",
  "TransactionID": "1467626",
  "Error Code": null,
  "GetSiteInfoTimeStamp": "2017-02-23 21:32:42"
}

在我的脚本中加载响应后:

ubdataset = json.loads(response.text)

如何只访问'aloha_mask'字段?

以下内容对我不起作用:

aloha = ubdataset['SiteInfo']['LOB03023D']['parameterList'][0]['aloha_mask']

提前感谢大家!

2 个答案:

答案 0 :(得分:2)

尝试将其更改为:

aloha = ubdataset['SiteInfo'][0]["'LOB03023D'"]['parameterList']['aloha_mask']

答案 1 :(得分:0)

参数列表不是列表,您的语句应为:

aloha = ubdataset['SiteInfo'][0]["'LOB03023D'"]['parameterList']['aloha_mask']

虽然请求无效json,但parameterList中的最后一个元素都有一个不应该存在的逗号。