Python解析有时可能为空的嵌套JSON值

Python解析有时可能为空的嵌套JSON值,python,json,Python,Json,我正试图解析以下内容,并将primary\u ip作为变量取出。有时主ip为“空”。下面是一个JSON示例、代码和我最近遇到的错误 { "count": 67, "next": "https://master.netbox.dev/api/dcim/devices/?limit=50&offset=50", "previous": null, "results": [

我正试图解析以下内容,并将
primary\u ip
作为变量取出。有时
主ip
为“空”。下面是一个JSON示例、代码和我最近遇到的错误

{
  "count": 67,
  "next": "https://master.netbox.dev/api/dcim/devices/?limit=50&offset=50",
  "previous": null,
  "results": [
   {
      "id": 28,
      "url": "https://master.netbox.dev/api/dcim/devices/28/",
      "name": "q2",
      "display_name": "q2",
      "device_type": {
        "id": 20,
        "url": "https://master.netbox.dev/api/dcim/device-types/20/",
        "manufacturer": {
          "id": 15,
          "url": "https://master.netbox.dev/api/dcim/manufacturers/15/",
          "name": "Zyxel",
          "slug": "zyxel"
        },
        "model": "GS1900",
        "slug": "gs1900",
        "display_name": "Zyxel GS1900"
      },
      "device_role": {
        "id": 4,
        "url": "https://master.netbox.dev/api/dcim/device-roles/4/",
        "name": "Access Switch",
        "slug": "access-switch"
      },
      "primary_ip": {
        "id": 301,
        "url": "https://master.netbox.dev/api/ipam/ip-addresses/301/",
        "family": 4,
        "address": "172.31.254.241/24"
      },
Python示例

import requests
import json

headers = {
    'Authorization': 'Token 63d421a5f733dd2c5070083e80df8b4d466ae525',
    'Accept': 'application/json; indent=4',
}

response = requests.get('https://master.netbox.dev/api/dcim/sites/', headers=headers)
j = response.json()

for results in j['results']:
    x=results.get('name')
    y=results.get('physical_address')

response2 = requests.get('https://master.netbox.dev/api/dcim/devices', headers=headers)
device = response2.json()
for result in device['results']:
  x=result.get('name')
  z=result.get('site')['name']
#  if result.get('primary_ip') != None
  y=result.get('primary_ip', {}).get('address')

  print(x,y,z)
我在运行时遇到以下错误:

ubuntu@ip-172-31-39-26:~$python3 Netbox python
回溯(最近一次呼叫最后一次):
文件“Netbox python”,第22行,在
y=result.get('primary_-ip',{}).get('address'))
AttributeError:“非类型”对象没有属性“get”

哪个值是
None
?它是
主ip
还是
地址

您可以尝试以下方法:

y=result.get('primary_-ip',{}).get('address,'empty_-address'))
这将用
空地址


更新:

我刚刚运行了您的代码,并获得了以下输出:

LC1 123.123.123.123/24 site1
q1 172.31.254.254/24 COD
q2 172.31.254.241/24 COD
运行此命令后:

import requests
import json

headers = {
    "Authorization": "Token 63d421a5f733dd2c5070083e80df8b4d466ae525",
    "Accept": "application/json; indent=4",
}

response = requests.get("https://master.netbox.dev/api/dcim/sites/", headers=headers)
j = response.json()

for results in j["results"]:
    x = results.get("name")
    y = results.get("physical_address")

response2 = requests.get("https://master.netbox.dev/api/dcim/devices", headers=headers)
device = response2.json()

for result in device["results"]:
    x = result.get("name")
    z = result.get("site")["name"]

    if result.get("primary_ip") != None:
        y = result.get("primary_ip").get("address")

        print(x, y, z)

我不确定预期的输出,但代码没有抛出任何错误。从代码的角度看,似乎有一些缩进错误,在缩进位置上不匹配。

哪个值是
None
?它是
主ip
还是
地址

您可以尝试以下方法:

y=result.get('primary_-ip',{}).get('address,'empty_-address'))
这将用
空地址


更新:

我刚刚运行了您的代码,并获得了以下输出:

LC1 123.123.123.123/24 site1
q1 172.31.254.254/24 COD
q2 172.31.254.241/24 COD
运行此命令后:

import requests
import json

headers = {
    "Authorization": "Token 63d421a5f733dd2c5070083e80df8b4d466ae525",
    "Accept": "application/json; indent=4",
}

response = requests.get("https://master.netbox.dev/api/dcim/sites/", headers=headers)
j = response.json()

for results in j["results"]:
    x = results.get("name")
    y = results.get("physical_address")

response2 = requests.get("https://master.netbox.dev/api/dcim/devices", headers=headers)
device = response2.json()

for result in device["results"]:
    x = result.get("name")
    z = result.get("site")["name"]

    if result.get("primary_ip") != None:
        y = result.get("primary_ip").get("address")

        print(x, y, z)

我不确定预期的输出,但代码没有抛出任何错误。从代码来看,似乎有一些缩进错误,东西在缩进位置上不匹配。

那么,前一行测试
None
有什么问题?前一行测试
None
有什么问题?对不起,地址值为null},“primary\u ip”:null,我现在得到这个错误ubuntu@ip-172-31-39-26:~$python3 Netbox python回溯(上次调用):文件“Netbox python”,第23行,y=result.get('primary_-ip',{})。get('address','empty_-address')AttributeError:'NoneType'对象没有属性'get'ubuntu@ip-172-31-39-26:~$你有没有运行我粘贴的内容?它似乎运行正常?这工作-非常感谢你!没问题,请你把这个答案标记为有用并打勾。:)抱歉-地址值为null},“primary_ip”:null,我现在得到这个错误ubuntu@ip-172-31-39-26:~$python3 Netbox-python回溯(最后一次调用):文件“Netbox-python”,第23行,y=result.get('primary_-ip',{}).get('address','empty_-address'))AttributeError:“非类型”对象没有属性“get”ubuntu@ip-172-31-39-26:~$你有没有运行我粘贴的内容?它似乎运行正常?这工作-非常感谢你!没问题,请你把这个答案标记为有用并打勾。:)