我想从currency converter API得到的Json响应中获取key和value,得到一个错误:“of string index must be integers”。下面是Python代码、Json中的数据和错误消息。在import json
from urllib.request import urlopen
with urlopen ("http://free.currencyconverterapi.com/api/v6/convert?q=ZAR_GBP,ZAR_USD")as response:
source=response.read()
data= json.loads(source)
data=json.dumps(data,indent=2)
print(data)
value= (data['results']['ZAR_GBP']['val'])
print(value)
Json输出为:
^{pr2}$
如果我想访问键“val”及其值,这会给我一个错误---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
5 data= json.loads(source)
6 data=json.dumps(data,indent=2)
----> 7 print (data['results']['ZAR_GBP']['val'])
8
TypeError: string indices must be integers