如何检查json对象是否具有某些属性 [英] How to check whether json object has some property
本文介绍了如何检查json对象是否具有某些属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java
中,有一个不错的方法has
,它可以检查json对象是否包含密钥.我这样使用它:
In Java
there is a nice method has
that makes it possible to check whether a json object contains a key or not. I use it like so:
JSONObject obj = ....; // <- got by some procedure
if(obj.has("some_key")){
// do something
}
在C#
的newtonsoft.json
库中找不到相同的出色功能.因此,我想知道有哪些替代方案.谢谢!
I could not find the same cool functionality in newtonsoft.json
library for C#
. So, I wonder what are the alternatives. Thanks!
推荐答案
只需使用obj ["proprty_name"].如果该属性不存在,则返回null
Just use obj["proprty_name"]. If the property doesn't exist, it returns null
if(obj["proprty_name"] != null){
// do something
}
这篇关于如何检查json对象是否具有某些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文