python中的Google Speech API JSON解析? [英] Google speech API JSON parsing in python?
本文介绍了python中的Google Speech API JSON解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了google json api的响应,并将文件保存在一个看起来像这样的json文件中
I have gotten the response of google json api and saved the file in a json file which looks like this
JSON文件我想在python 2中解析它.我已经尝试过
And I want to parse it in python 2. I've tried
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
Use
json.loads
to convert / parsestr
todict
"alternatives"
的类型为list
如果您的数据来自json文件,请先阅读
If your data is coming from a json file, read it first
with open('data.json', 'r') as f: data = f.read() # refer to above code
这篇关于python中的Google Speech API JSON解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文