我想解析下面的JSON,並從元素中提取名稱和間隔。解析Python JSON
reply "[ {
"interface" : [ {
"name" : "ethernet39",
"number" : 39,
"rate" : [ {
"interval" : 45,
"rx-bad-vlan-rate" : 0,
"rx-broadcast-packet-rate" : 0,
"rx-byte-rate" : 0,
"rx-drop-rate" : 0,
"rx-error-rate" : 0,
"rx-multicast-packet-rate" : 0,
"rx-unicast-packet-rate" : 0,
"timestamp" : "2015-06-18T21:59:23.703Z",
"tx-broadcast-packet-rate" : 0,
"tx-byte-rate" : 0,
"tx-drop-rate" : 0,
"tx-error-rate" : 0,
"tx-multicast-packet-rate" : 0,
"tx-unicast-packet-rate" : 0
}, {
"interval" : 45,
"rx-bad-vlan-rate" : 0,
"rx-broadcast-packet-rate" : 0,
"rx-byte-rate" : 0,
"rx-drop-rate" : 0
...
我已經試過如下:
for x in range(0,len(interfaces)):
for interface in interfaces[x]:
entry = {}
entry['name'] = statistic['name']
for interval in statistic['rate']:
entry['byte_rate'] = interval['rx-byte-rate']
entry['packet_rate'] = interval['rx-unicast-packet-rate']
entry['timestamp'] = interval['timestamp']
entry['droprate'] = interval['rx-drop-rate']
entry['errorrate'] = interval['rx-error-rate']
entries.append(entry)
不過,我總是得到一個錯誤, 類型錯誤:字符串索引必須是整數
我在做什麼錯誤?在代碼中,接口是通過執行json.load獲取的JSON()
您可能想要顯示錯誤跟蹤。 [問] – boardrider