{ "name": "John Doe", "age": 30, "email": "[email protected]" }
JObject obj = JObject.Parse(json); bool hasAge = obj.ContainsKey("age"); if (hasAge) { Console.WriteLine("The object contains the key 'age'"); } else { Console.WriteLine("The object does not contain the key 'age'"); }
{ "name": "John Doe", "age": 30, "address": { "street": "123 Main St", "city": "Anytown", "state": "CA" } }
JObject obj = JObject.Parse(json); JObject address = (JObject)obj["address"]; bool hasState = address.ContainsKey("state"); if (hasState) { Console.WriteLine("The 'address' object contains the key 'state'"); } else { Console.WriteLine("The 'address' object does not contain the key 'state'"); }Package library: JSON.NET (Newtonsoft.Json).