python中的Google语音API JSON解析?
我得到了google json api的响应,并将该文件保存在一个json文件中,如下所示 JSON文件 我想在python 2中解析它。我试过了python中的Google语音API JSON解析?,python,json,Python,Json,我得到了google json api的响应,并将该文件保存在一个json文件中,如下所示 JSON文件 我想在python 2中解析它。我试过了 for result in response.results: # The first alternative is the most likely one for this portion. print('Transcript: {}'.format(result.alternatives[0].transcript
for result in response.results:
# The first alternative is the most likely one for this portion.
print('Transcript: {}'.format(result.alternatives[0].transcript))
print('Confidence: {}'.format(result.alternatives[0].confidence))
但它抛出了错误
'str' object has no attribute 'results'
后来我试过了
jsondata = json.load(json_file_path)
但是它说
'str' object has no attribute 'read'
有什么帮助吗?试试这个:
data = "your json data" # of type `str`
json_dict = json.loads(data)
for result in json_dict["response"]["results"]:
if "alternatives" in result:
alternatives = result["alternatives"][0]
if "confidence" in alternatives:
print(alternatives["confidence"])
if "transcript" in alternatives:
print(alternatives["transcript"])
- 使用
将json.loads
转换/解析为str
dict
属于“备选方案”
列表类型
with open('data.json', 'r') as f:
data = f.read()
# refer to above code
为什么此代码无法处理此文件无法访问它,请立即尝试,我已提供了访问权限最终解决了问题,感谢您的帮助:)