【发布时间】:2019-03-14 04:53:43
【问题描述】:
如何在代码中获取速度值?
{
"response": {
"speed": "1000000",
"lastUpdated": "2018-10-09 11:19:10.981000",
"portMode": "access",
"portType": "Ethernet Port",
"description": " changed by DNAC",
"series": "Cisco Catalyst 9300 Series Switches",
"ifIndex": "31",
"mediaType": "100/1000/2.5G/5G/10GBaseTX",
"className": "SwitchPort",
"interfaceType": "Physical",
"ipv4Address": null,
"ipv4Mask": null,
"isisSupport": "false",
"mappedPhysicalInterfaceId": null,
"mappedPhysicalInterfaceName": null,
"nativeVlanId": "1",
"ospfSupport": "false",
"pid": "C9300-24UX",
"serialNo": "FCW2140L039",
"voiceVlan": null,
"status": "up",
"deviceId": "4757da48-3730-4833-86db-a0ebfbdf0009",
"macAddress": "f8:7b:20:71:4d:98",
"portName": "TenGigabitEthernet1/0/24",
"adminStatus": "UP",
"duplex": "FullDuplex",
"vlanId": "1",
"instanceTenantId": "5b13fcf9651f93008acd0702",
"instanceUuid": "8beed342-3718-4fa9-8377-4c7d94c77aef",
"id": "8beed342-3718-4fa9-8377-4c7d94c77aef"
},
"version": "1.0"
}
我尝试了下面的代码,但它不起作用。
for items in data["response"]
print(items['speed'])
这会给我错误 TypeError: 字符串索引必须是整数
【问题讨论】:
-
只是
data["response"]["speed"] -
低质量问题的低质量欺骗候选人:Python - accessing values nested within dictionaries
-
@Aran-Fey 你对我太好了。关闭,下次我会删除我的答案:)
-
@Jean-FrançoisFabre 好吧,老实说,我对这个问题作为欺骗目标不太满意。这个问题有点不清楚,所以答案到处都是。并且解释为什么 OP 的代码也会抛出 TypeError 也有一些价值。因此,总的来说,发布答案是 IMO 的合理选择。
标签: python json python-3.x