Simplejson和随机键值 [英] Simplejson and random key value

查看:107
本文介绍了Simplejson和随机键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从API服务器获得的价值

Here is the value i got from API server

{"query":{"pages":{-1":{"ns":0,"title":"spencerx","missing":"}}}}

{"query":{"pages":{"-1":{"ns":0,"title":"spencerx","missing":""}}}}

让我说如果我想确定它是否不缺字,我将通过查看申报表的"-1"来知道.但是当这个词存在时,它将返回以下json

Let say if i want to get determine if it's not missing word, i will know by looking at "-1" of the return. but when the word exist, it will returning me the following json

{"query":{"pages":{"1080152":{"pageid":1080152,"ns":0,"title":"spencer"}}}}

{"query":{"pages":{"1080152":{"pageid":1080152,"ns":0,"title":"spencer"}}}}

是一个随机数.我可以知道如何检测到"-1"并确定该词不存在吗?当我尝试打印x ['query'] ['pages']时,它只会将以下所有内容抛给我,但是我不知道如何检测到它的关键错误.谢谢.

which is a random number. may i know how could i detect that's '-1' and determine the word doesn't exist? while i try to print x['query']['pages'] it will just throw all the following behind to me, but i don't know how to detect it's key error. thanks.

推荐答案

尝试print x['query']['pages'].keys(),对于第一种情况,您会得到['-1'],对于第二种情况将得到['1080152'].

try print x['query']['pages'].keys(), which would give you ['-1'] for the first case and ['1080152'] for the second one.

如果您只想检查x['query']['pages']中的'-1':

if '-1' in x['query']['pages']:
   # dictionary x['query']['pages'] has '-1' as a key

足够了

这篇关于Simplejson和随机键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆