Reputation: 324
I am getting an Error in the by me created JSON by the message: string indices must be integers. I did read some topics on stackoverflow about this issue, but it is not clear for me what I have to change.
The issue is with items = json_data['items']
My code:
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']
Upvotes: 0
Views: 72
Reputation: 5785
Try this, You need to use json.loads
to bring it into right format(i.e., dictionary)
>>> data = json.loads(json_data)
>>> data['items']
'items' # your output
Upvotes: 1