python中json格式检查_Python检查带有变量的json文件

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'])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值