Python判断json是否有key

string =" {string =" {
  "status": "error",
  "messages": ["Could not find resource or operation 'BZK1.MapServer' on the system."],
  "code": 404
}"

print  '对象:' string

输出结果为:

对象:

{u'status': u'error', u'code': 404, u'messages': [u"Could not find resource or operation 'BZK1.MapServer' on the system."]}
jsonOB=json.load(string)

判断json中是否有某个key 

 jsonOB.has_key('code')

上边使用的python版本为python2.7

Python3中使用:

 使用 in 操作符用于判断键是否存在于字典中,如果键在字典 dict 里返回 true,否则返回 false。

而 not in 操作符刚好相反,如果键在字典 dict 里返回 false,否则返回 true。

dict = {'Name': 'Runoob', 'Age': 7} 

# 检测键 Age 是否存在 
if 'Age' in dict: 
   print("键 Age 存在") 
else : 
   print("键 Age 不存在") 

# 检测键 Sex 是否存在 
if 'Sex' in dict:
    print("键 Sex 存在") 
else :
    print("键 Sex 不存在") 

# not in # 检测键 Age 是否存在 
if 'Age' not in dict: 
   print("键 Age 不存在")
else : 
   print("键 Age 存在")

输出结果:

键 Age 存在

键 Sex 不存在

键 Age 存在

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值