我已经从google speech获得了一个变量的结果
data = {'name': '1235433175192040985', 'metadata': {'@type': 'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata', 'progressPercent': 100, 'startTime': '2018-04-11T12:56:58.237060Z', 'lastUpdateTime': '2018-04-11T12:57:44.944653Z'}, 'done': true, 'response': {'@type': 'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse', 'results': [{'alternatives': [{'transcript': 'hi how are you', 'confidence': 0.92438406}]}, {'alternatives': [{'transcript': 'How are you doing?', 'confidence': 0.9402676}]}]}}
json_dict = json.loads(data)
在这个问题上,它抛出了错误
^{pr2}$剩下的解析我写了
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"])
我做错什么了?在
Python中的JSON解析器希望blob使用双引号,因为这是JSON standard。在
您可以用双引号替换单引号,如this answer中所述。在
不过,我很确定这个问题可以在其他地方解决,因为googleapi很可能在其响应中使用了有效的JSON。如何解析来自Google API的响应?在
代码片段中的问题是,您将dict传递给json.loads. json.loads将json解码为dict,因此它是多余的和错误的。 read the docs
dict不需要任何进一步的json方法,您可以按原样使用它。在
产生以下输出:
^{pr2}$相关问题 更多 >
编程相关推荐