Skip to main content
 首页 » 编程设计

python之使用Python访问JSON对象中包含的列表

2025年05月04日8emanlee

testVar = { 
  "date": 1520053431, 
  "location": { 
  "lat": 39.349887, 
  "lng": -82.116147 
  }, 
  "items": [ 
    { 
      "macAddress": "98:90:96:9C:94:6C", 
      "manufacturer": "intel" 
    }, 
    { 
  "macAddress": "98:90:96:9C:94:52", 
  "manufacturer": "intel" 
    }, 
    { 
      "macAddress": "98:90:96:90:94:52", 
      "manufacturer": "intel" 
    } 
  ] 
} 
 
print(testVar.items[0]) 


我有这个JSON对象,并且我正在尝试访问项目列表,但这会产生错误:

TypeError: 'builtin_function_or_method' object is not subscriptable 


我试图用json.loads和json.dumps解析它,但是它们都不起作用。

请您参考如下方法:

由于带有大括号({...}),因此将其存储在dict(离子)中。这意味着您不能使用点运算符(.)访问内部数据。

由于它是dict,因此您也不能像list那样访问它。相反,您需要使用可用的键(冒号之前的字符串)。

在这种情况下,您需要执行textVar['items']之类的操作来访问'items' list。