Python 使用json数据解析错误:字符串索引必须是整数

Python 使用json数据解析错误:字符串索引必须是整数,python,json,python-3.x,Python,Json,Python 3.x,我正在尝试解析Json。我已经放置了json结构的图像。请看一看 我收到的错误是: print("title: "+json_data["title"]) TypeError: string indices must be integers 我的JSON结构如下: [ [ { "title": "What you need to know", "snippet": "+128.2.207.79; path=/resourc

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

我收到的错误是:

print("title: "+json_data["title"])
TypeError: string indices must be integers
我的JSON结构如下:

[
    [
        {
            "title": "What you need to know",
            "snippet": "+128.2.207.79; path=/resources/useful-links/43-what-<B>you</B>-<B>need</B>-to-<B>know</B>-amy\nLast-Modified: Sun, 18 Mar <B>you</B> <B>need</B> to <B>know</B> \n\n 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\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews it!\tTheir story\n\n\n\tBlogs\tThe Oops! Team’s\n\tFrom the Experts\n\n\n\tResources\tVideos\n\tInfo Sheets\n\tAll <B>you</B>",
            "id": "clueweb12-1007wb-23-25678",
            "url": "174.142.68.174",
            "score": 13.533853530883789
        }
    ],
    [
        {
            "title": "What you need to know ",
            "snippet": "  What <B>you</B> <B>need</B> to <B>know</B> (Jessica)  \n\n\n\n    \n\n\n\n\n\n\n\n\t\t\n\n    \n\n\n\n\n\n   \n    Share <B>you</B> <B>need</B> to <B>know</B> \n\n Sports involving jumping or impact have a direct effect...\n\n\n The Oops! team's blog\n\n \n\nVideos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor it!\tTheir story\n\n\n\tBlogs\tThe Oops! Team’s\n\tFrom the Experts\n\n\n\tResources\tVideos\n\tInfo Sheets\n\tAll <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews\tPress",
            "id": "clueweb12-1007wb-27-29239",
            "url": "174.142.68.174",
            "score": 13.533853530883789
        }
    ],
    [
        {
            "title": "What you need to know",
            "snippet": "+128.2.207.79; path=/component/content/article/46-what-<B>you</B>-<B>need</B>-to-<B>know</B>-\nLast-Modified: Sat, 17 <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews <B>you</B> <B>need</B> to <B>know</B>...\n\n\n The Oops! team's blog\n\n \n\nVideos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor <B>need</B> to <B>know</B>\n\nFor more information",
            "id": "clueweb12-1006wb-19-18957",
            "url": "174.142.68.174",
            "score": 13.533853530883789
        }
    ],
    {
        "hasmore": 1,
        "numhits": 1000
    }
  ]

]

问题是因为最后的
命令

{
    "hasmore": 1,
    "numhits": 1000
}
您的内部循环希望处理包含dict的列表,因此当它点击该dict时,它会尝试在“hasmore”键上执行
json_data[“title”]

这是一个修复过的版本

import json

datastring = r'''
[
    [
        {
            "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\nLast-Modified: Sun, 18 Mar <B>you</B> <B>need</B> to <B>know</B> (Amy)\n\n 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\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews it!\tTheir story\n\n\n\tBlogs\tThe Oops! Team\u2019s\n\tFrom the Experts\n\n\n\tResources\tVideos\n\tInfo Sheets\n\tAll <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)  \n\n\n\n    \n\n\n\n\n\n\n\n\t\t\n\n    \n\n\n\n\n\n   \n    Share <B>you</B> <B>need</B> to <B>know</B> (Jessica)\n\n Sports involving jumping or impact have a direct effect...\n\n\n The Oops! team's blog\n\n \n\nVideos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor it!\tTheir story\n\n\n\tBlogs\tThe Oops! Team\u2019s\n\tFrom the Experts\n\n\n\tResources\tVideos\n\tInfo Sheets\n\tAll <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews\tPress",
            "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\nLast-Modified: Sat, 17 <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews <B>you</B> <B>need</B> to <B>know</B>...\n\n\n The Oops! team's blog\n\n \n\nVideos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor <B>need</B> to <B>know</B>\n\nFor more information",
            "id": "clueweb12-1006wb-19-18957",
            "url": "174.142.68.174",
            "score": 13.533853530883789
        }
    ],
    {
        "hasmore": 1,
        "numhits": 1000
    }
]
'''

data = json.loads(datastring)

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

您没有使用
json.loads
将字符串解析到字典中。使用了json.loadWhat@JimFasarakisHilliard所说的,或者
数据
没有您认为的结构。在内部for循环之前添加
print(json\u inner\u array)
,查看第二个循环实际迭代的内容。“used the json.load”编辑您的问题并向我们展示您是如何使用它的。此时,您提供的代码片段没有显示
json
库的任何用法。有一点是肯定的:如果您收到此消息,那么此时
json\u数据
是一个字符串。FWIW
json
拒绝解析您发布的json片段。请贴一张合适的MCVE@AbhishekKaushik您是否有可能更改返回的JSON(或请求更改)?如果数组中的每个值都具有相同的类型,那么JSON的解析就会更加清晰。@AbhishekKaushik我同意chepner的观点,如果你能改变JSON结构,你应该这样做!依我看,外层应该是一个目录,而不是一个列表。该dict可以包含一个用于额外信息的dict和一个主要数据dict的列表,没有必要将每个dict包装在自己的1项列表中。@chepner谢谢,但我无法更改结构,因为我是从其他人那里获得的,用于执行一些实验。。我在这个Json中发现了困难。结构不是很清楚。谢谢大家的帮助和支持。
import json

datastring = r'''
[
    [
        {
            "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\nLast-Modified: Sun, 18 Mar <B>you</B> <B>need</B> to <B>know</B> (Amy)\n\n 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\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews it!\tTheir story\n\n\n\tBlogs\tThe Oops! Team\u2019s\n\tFrom the Experts\n\n\n\tResources\tVideos\n\tInfo Sheets\n\tAll <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)  \n\n\n\n    \n\n\n\n\n\n\n\n\t\t\n\n    \n\n\n\n\n\n   \n    Share <B>you</B> <B>need</B> to <B>know</B> (Jessica)\n\n Sports involving jumping or impact have a direct effect...\n\n\n The Oops! team's blog\n\n \n\nVideos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor it!\tTheir story\n\n\n\tBlogs\tThe Oops! Team\u2019s\n\tFrom the Experts\n\n\n\tResources\tVideos\n\tInfo Sheets\n\tAll <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews\tPress",
            "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\nLast-Modified: Sat, 17 <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor\n\tFrequently Asked Questions\n\tGlossary\n\tUseful links\n\n\n\tNews <B>you</B> <B>need</B> to <B>know</B>...\n\n\n The Oops! team's blog\n\n \n\nVideos Info sheets All <B>you</B> <B>need</B> to <B>know</B> about your pelvic floor <B>need</B> to <B>know</B>\n\nFor more information",
            "id": "clueweb12-1006wb-19-18957",
            "url": "174.142.68.174",
            "score": 13.533853530883789
        }
    ],
    {
        "hasmore": 1,
        "numhits": 1000
    }
]
'''

data = json.loads(datastring)

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