Manipulating json files python

时光总嘲笑我的痴心妄想 提交于 2019-12-23 20:13:58

问题


I'm receiving a JSON file with a lot of information, and I'm trying to get some properties of this JSON file, I research at Python API and I couldn't find any information about some function that search at the JSON file or the strings that's loaded with the "loads" method, someone knows some way to get the specified information by a simple and clean operation?

EDIT

Follow the JSON file after executed the loads function:

{
  "servers": [
    {
      "status": "ACTIVE",
      "updated": "2012-01-02T20:46:21Z",
      "hostId": "ID",
      "user_id": "ID",
      "name": "Serve",
      "links": [
        {
          "href": "URL",
          "rel": "self"
        },
        {
          "href": "URL",
          "rel": "bookmark"
        }
      ],
      "addresses": {
        "nuvemcpca": [
          {
            "version": 4,
            "addr": "10.0.0.2"
          }
        ]
      },
      "tenant_id": "nuvemcpca",
      "image": {
        "id": "15",
        "links": [
          {
            "href": "URL",
            "rel": "bookmark"
          }
        ]
      },
      "created": "2012-01-02T20:19:04Z",
      "uuid": "1710c4bb-3d48-49f9-b88e-2a19355de199",
      "accessIPv4": "",
      "accessIPv6": "",
      "key_name": "name",
      "progress": 100,
      "flavor": {
        "id": "1",
        "links": [
          {
            "href": "URL",
            "rel": "bookmark"
          }
        ]
      },
      "config_drive": "",
      "id": 15,
      "metadata": {

      }
}

I want to get the status information.


回答1:


Your data doesn't seem to be properly formatted, in particular I don't see where's the closing square bracket for the one opening just after "servers":, but I'd say that what you're looking for is:

data['servers'][0]['status']

where data is the variable that points to the result of json.loads.



来源:https://stackoverflow.com/questions/8712008/manipulating-json-files-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!