Simplejson and random key value

核能气质少年 提交于 2019-12-11 14:08:33

问题


Here is the value i got from API server

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

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"}}}}

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.


回答1:


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

if you just want to check '-1' in x['query']['pages']:

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

would suffice



来源:https://stackoverflow.com/questions/15261465/simplejson-and-random-key-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!