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

时间:2019-07-12 19:33:00

标签: json python-3.x

我有以下JSON数据

mail.google.com

如果我尝试 { "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["results"],它将无法正常工作。

我想获取“成绩单”中的文字,我该如何获取?

3 个答案:

答案 0 :(得分:1)

"transcripts"不是data的直接子级。相反,它是元素"alternatives"的子元素,它是列表"results"的每个元素的子元素,而列表data依次是transcripts = [r["alternatives"]["transcript"] for r in data["results"]] 的直接子元素。因此,要将您的笔录内容作为列表获取,请执行以下操作:

g = df.groupby(['case','constant','code'])

df_out = df.set_index(['case','constant','code',g.cumcount()+1]).unstack(fill_value=0)
df_out.columns = [f'{i}{j}' for i, j in df_out.columns]
df_out.reset_index()

答案 1 :(得分:0)

要访问替代项,

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

根据需要提取的笔录数据更改索引0、1、2、3...。

答案 2 :(得分:0)

您可以使用以下代码获得预期结果:

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)