Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的Google语音API JSON解析?_Python_Json - Fatal编程技术网

python中的Google语音API JSON解析?

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

我得到了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))
        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

  • “备选方案”
    属于
    列表类型

如果数据来自json文件,请先读取它

with open('data.json', 'r') as f:
    data = f.read()
    # refer to above code

为什么此代码无法处理此文件无法访问它,请立即尝试,我已提供了访问权限最终解决了问题,感谢您的帮助:)