But I have more json files who have different number of these substrings. So I did this to find how many there are in the text:
import json
json_str = open('jsonfile.txt', 'r').read()
contact = json.loads(json_str)
So GraphImages_total is 18
.
Every substring has comments --> data --> 0 --> owner --> username
So I want to print the username.
comment_author = contact["GraphImages"][0]["comments"]["data"][0]["owner"]["username"]
print(comment_author)
This is for GraphImages_total = 0
But I want to do it for all of them.
So I need a way to make it like this:
for graph_image in contact['GraphImages']:
comment_author = contact["GraphImages"][graph_image]["comments"]["data"][0]["owner"]["username"]
print(comment_author)
But I get this error:
comment_author = contact["GraphImages"][graph_image]["comments"]["data"][0]["owner"]["username"]IndexError: list index out of range
解决方案contact["GraphImages"][0]["comments"]["data"][0]["owner"]["username"]
^ ^
This indicates where the lists of unknown length are. The GraphImages and data keys hold lists. To iterate over a list, you use a for .. in statement as follows:
my_list = ['foo', 'bar', 'baz']
for item in my_list:
print(item)
Note that you're using item directly. item will in turn become 'foo', 'bar' and 'baz' on each respective iteration of the loop. You don't need to use any numeric indices nor count up any x or y.
Applied to your situation, you need two such loops:
for graph_image in contact['GraphImages']:
for comment in graph_image['comments']['data']:
print(comment['text'], comment['owner']['username'])