has() method – Class JsonObject. This is the convenience method that can be used to check of a property/member with the specified key is present in the JsonObject or not. This method returns true if the member with specified key exists, otherwise it returns false.
To check whether a property exists on a JObject , you can use the square bracket syntax and see whether the result is null or not. If the property exists, a JToken will be always be returned (even if it has the value null in the JSON).
So you see, a JObject is a JContainer , which is a JToken . Here's the basic rule of thumb: If you know you have an object (denoted by curly braces { and } in JSON), use JObject. If you know you have an array or list (denoted by square brackets [ and ] ), use JArray.
The Newtonsoft. JSON namespace provides classes that are used to implement the core services of the framework. It provides methods for converting between . NET types and JSON types.
Just use x["error_msg"]
. If the property doesn't exist, it returns null
.
JObject
implements IDictionary<string, JToken>
, so you can use:
IDictionary<string, JToken> dictionary = x;
if (dictionary.ContainsKey("error_msg"))
... or you could use TryGetValue
. It implements both methods using explicit interface implementation, so you can't use them without first converting to IDictionary<string, JToken>
though.
JObject.ContainsKey(string propertyName)
has been made as public method in 11.0.1 release
Documentation - https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_ContainsKey.htm
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