我在由我创建的JSON中得到一个错误,消息是:字符串索引必须是整数。我确实读了一些关于stackoverflow的话题,但是我不清楚我要改变什么
问题在于items = json_data['items']
我的代码:
formattedUrl = ["https://stackoverflow.com/", "https://en.wikipedia.org/wiki/Main_Page"]
displayLink = ["https://stackoverflow.com/", "https://en.wikipedia.org/wiki/Main_Page"]
htmlFormattedUrl = ["https://stackoverflow.com/", "https://en.wikipedia.org/wiki/Main_Page"]
title = ["Stackoverflow", "Wikipedia, the free encyclopedia"]
htmlTitle = ["Stackoverflow", "Wikipedia, the free encyclopedia"]
snippet = ["Stack Overflow is the largest", "Main page"]
#
keys = ['formattedUrl', 'displayLink', 'htmlFormattedUrl', 'title', 'htmlTitle', 'snippet']
items = [dict(zip(keys, [u, t, d, aa, ab, ac])) for u, t, d, aa, ab, ac in
zip(formattedUrl, displayLink, htmlFormattedUrl, title, htmlTitle, snippet)]
d = {
'items': items
}
json_data = json.dumps(d, indent=4)
#queries
if has_result == 1 :
# print "results"
result = []
results = []
items = json_data['items']
试试这个,您需要使用
json.loads
将它转换成正确的格式(即字典)相关问题 更多 >
编程相关推荐