简体   繁体   English

Google Speech API json解析?

[英]Google speech API json parsing?

I have obtained the results from google speech in a variable 我已经从Google语音中获得了一个变量的结果

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)

On this it throws error 在此抛出错误

JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

For the rest of parsing I wrote 对于其余的解析,我写了

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"])

What am I doing wrong? 我究竟做错了什么?

The JSON parser in Python expects your blob to use double quotation marks, since that is the JSON standard . Python中的JSON解析器希望您的Blob使用双引号,因为这是JSON standard

{
  "name": "John Doe"
}

You could replace the single quotes with double quotes, as explained in this answer . 您可以用双引号代替单引号,如本答案所述

However, I'm pretty sure the problem can be solved elsewhere, since the Google API most likely uses valid JSON in it's responses. 但是,我非常确定该问题可以在其他地方解决,因为Google API最有可能在响应中使用有效的JSON。 How do you parse the response from Google's API? 您如何解析Google API的响应?

The problem in your snippet is that you're passing a dict to json.loads. 您的代码段中的问题是您要将字典传递给json.loads。 json.loads decodes json to dict, so it redundant and wrong. json.loads将json解码为字典,因此它是多余的和错误的。 read the docs 阅读文档

The dict doesn't need any further json methods, you can work with it as is. 该dict不需要任何其他json方法,您可以按原样使用它。

for result in data["response"]["results"]:
  if "alternatives" in result:
    alternatives = result["alternatives"][0]
    if "confidence" in alternatives:
      print(alternatives["confidence"])
    if "transcript" in alternatives:
      print(alternatives["transcript"])

Yields this output: 产生以下输出:

0.92438406
hi how are you
0.9402676
How are you doing?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM