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

阿布舍克考希克

我正在尝试解析 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
    }
  ]

]

虽然我使用的代码是:

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

#assigning path to the url variable
url = link of the server 
#Taking response and request  from url
r = urllib.request.urlopen(url)
#reading and decoding the data
data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))


for json_inner_array in data:
        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'])
PM 2环

问题是因为那个 final dict

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

您的内部循环期望处理包含字典的列表,因此当它遇到该字典时,它会尝试json_data["title"]在“hasmore”键上执行操作。

这是修复版。

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

如果您需要读取最终字典中的数据,只需添加一个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"])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:使用Python解析JSON时,字符串索引必须是整数?

JSON字符串索引必须为整数

解析JSON时,字符串索引必须为整数-python

TypeError:使用Python解析Json时,字符串索引必须是整数错误

处理使用'http'下载的JSON时发生错误-TypeError:字符串索引必须为整数

TypeError:字符串索引必须是jSON和Flask的整数错误

Python:JSON规范化“字符串索引必须为整数”错误

Django序列化器从帖子访问json数据(字符串索引必须为整数)

TypeError:字符串索引必须是整数-json

字符串索引必须是整数json python

使用jq解析JSON数据时出现错误消息“无法使用字符串“标题”索引数组”

python pandas - 解析 JSON 时出现类型错误:字符串索引必须是整数

类型错误:字符串索引必须是整数,而不是带有 JSON 解析的 str

json.loads TypeError:字符串索引必须是整数

类型错误:字符串索引必须是整数,而使用 Gerrit rest API 在 pyspark 中提取数据

Robot Framework - 类型错误:解析 Json 时字符串索引必须是整数

类型错误:字符串索引必须是带 JSON 的整数

类型错误:字符串索引必须是复杂的 json 数组中的整数

嵌套的 JSON,字符串索引必须是整数

使用 Gson 解析 Json 字符串给我错误

类型错误:处理 json 文件时字符串索引必须是整数

JSON:TypeError: 字符串索引必须是整数

类型错误:json.loads() 之后的字符串索引必须是整数

类型错误:在 Python 中过滤 JSON 中的值时,字符串索引必须是整数

JSON 注释错误“字符串索引必须是整数”

json数据处理错误“字符串索引必须是整数”

解析 JSON 时出现“字符串索引必须是整数”错误

TypeError:字符串索引必须是整数 JSON 文件

字符串索引必须是整数,同时通过json解析