我正在尝试使用 Simplejson 在 python 中提取 JSON 对象。但我收到以下错误。
Traceback (most recent call last):
File "Translator.py", line 42, in <module>
main()
File "Translator.py", line 38, in main
parse_json(trans_text)
File "Translator.py", line 27, in parse_json
result = json['translations']['translatedText']
TypeError: list indices must be integers, not str
这是我的JSON
对象的样子,
{'translations': [{'translatedText': 'fleur'}, {'translatedText': 'voiture'}]}
这是我的python代码。
def parse_json(trans_text):
json = simplejson.loads(str(trans_text).replace("'", '"'))
result = json['translations']['translatedText']
print result
有什么想法吗?