我正在尝试解析 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'])
问题是因为那个 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] 删除。
我来说两句