简体   繁体   English

如何从此Json数据中获取特定数据?

[英]How do I get specific data from this Json data?

I have the following JSON data 我有以下JSON数据

    {
    "results": [
        {
            "alternatives": [
                {
                    "confidence": 0.6,
                    "transcript": "state radio "
                }
            ],
            "final": true
        },
        {
            "alternatives": [
                {
                    "confidence": 0.77,
                    "transcript": "tomorrow I'm headed to mine nine 
    consecutive big con I'm finna old tomorrow I've got may meet and greet 
    with whoever's dumb enough to line up "
                }
            ],
            "final": true

If I try data["results"] , it works and I get everything inside "results". 如果我尝试data["results"] ,它会起作用,并且我将所有内容都放入“ results”中。

But if I try data["alternatives"] , it doesn't work. 但是,如果我尝试使用data["alternatives"] ,它将无法正常工作。

I want to get the text in "transcript", how can I get that? 我想获取“成绩单”中的文字,我该如何获取?

"transcripts" is not a direct child of data . "transcripts"不是data的直接子代。 It is, instead, the child of element "alternatives" , which is a child of each element of the list "results" , which is, in turn, the direct child of data . 相反,它是元素"alternatives"的子元素,它是列表"results"的每个元素的子元素,而"results"又是data的直接子元素。 So, to get your contents of transcript as a list, do: 因此,要将您的笔录内容作为列表获取,请执行以下操作:

transcripts = [r["alternatives"]["transcript"] for r in data["results"]]

To access alternatives, 要访问替代方案,

data['results'][0]['alternatives']['transcript]

change the index 0,1,2,3 ... according to which transcript data you need to extract. 根据您需要提取的笔录数据更改索引0、1、2、3...。

You can get the expected result by using the code: 您可以使用以下代码获得预期的结果:

import json
d='''
    {
    "results": [
        {
            "alternatives": [
                {
                    "confidence": 0.6,
                    "transcript": "state radio "
                }
            ],
            "final": true
        },
        {
            "alternatives": [
                {
                    "confidence": 0.77,
                    "transcript": "tomorrow I'm headed to mine nine consecutive big con I'm finna old tomorrow I've got may meet and greet with whoever's dumb enough to line up "
                }
            ],
            "final": true
        }
    ]}
'''
data = json.loads(d)
for i in range(len(data['results'])):
    transcript=data['results'][i]['alternatives'][0]['transcript']
    print(transcript)

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

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