分析时出现json数据错误:字符串索引必须是整数

2024-04-23 11:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解析Json。我已经把json结构的图像。请看一下。在

我收到的错误是:

print("title: "+json_data["title"])
TypeError: string indices must be integers

我的JSON结构如下:

^{pr2}$

我使用的代码是:

这是我加载json数据的代码。在

^{3}$

Tags: integers代码图像jsondatastringtitle错误
1条回答
网友
1楼 · 发布于 2024-04-23 11:11:13

问题是因为最后的dict

{
    "hasmore": 1,
    "numhits": 1000
}

您的内部循环期望处理包含dict的列表,因此当它命中该dict时,它试图对“hasmore”键执行json_data["title"]。在

这是一个修复版本。在

^{pr2}$

输出

title: What you need to know (Amy)
snippet:  +128.2.207.79; path=/resources/useful-links/43-what-<B>you</B>-<B>need</B>-to-<B>know</B>-amy
Last-Modified: Sun, 18 Mar <B>you</B> <B>need</B> to <B>know</B> (Amy)

 Urinary stress incontinence is common after delivery; in fact, some All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor <B>need</B> to <B>know</B> about your pelvic floor
        Frequently Asked Questions
        Glossary
        Useful links


        News it!        Their story


        Blogs   The Oops! Team’s
        From the Experts


        Resources       Videos
        Info Sheets
        All <B>you</B>
id:  clueweb12-1007wb-23-25678
url:  174.142.68.174
score: 13.533853530883789
title: What you need to know (Jessica)
snippet:    What <B>you</B> <B>need</B> to <B>know</B> (Jessica)  




















    Share <B>you</B> <B>need</B> to <B>know</B> (Jessica)

 Sports involving jumping or impact have a direct effect...


 The Oops! team's blog



Videos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor it!        Their story


        Blogs   The Oops! Team’s
        From the Experts


        Resources       Videos
        Info Sheets
        All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor
        Frequently Asked Questions
        Glossary
        Useful links


        News    Press
id:  clueweb12-1007wb-27-29239
url:  174.142.68.174
score: 13.533853530883789
title: What you need to know (Susan)
snippet:  +128.2.207.79; path=/component/content/article/46-what-<B>you</B>-<B>need</B>-to-<B>know</B>-susan
Last-Modified: Sat, 17 <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor
        Frequently Asked Questions
        Glossary
        Useful links


        News <B>you</B> <B>need</B> to <B>know</B>...


 The Oops! team's blog



Videos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor <B>need</B> to <B>know</B>

For more information
id:  clueweb12-1006wb-19-18957
url:  174.142.68.174
score: 13.533853530883789

如果需要读取最后dict中的数据,只需添加一个else块。例如

for json_inner_array in data:
    if isinstance(json_inner_array, list):
        for json_data in json_inner_array:
            print("title: "+json_data["title"])
            print("snippet: ", json_data["snippet"])
            print("id: ",json_data["id"])
            print("url: ",json_data["url"])
            print("score:",json_data['score'])
    else:
        print("\nExtra info:")
        print("hasmore:", json_inner_array["hasmore"])
        print("numhits:", json_inner_array["numhits"])

相关问题 更多 >