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
}
I could not find the same cool functionality in newtonsoft.json
library for C#
. So, I wonder what are the alternatives. Thanks!
Just use obj["proprty_name"]. If the property doesn't exist, it returns null
if(obj["proprty_name"] != null){
// do something
}
You can try like this:
IDictionary<string, JToken> dict = x;
if (dict.ContainsKey("some_key"))
since JSONObject
implements IDictionary<string, JToken>
. You can refer MSDN for details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With