How to check if JSONValue of type object has a key?

Fusxfaranto via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 6 14:39:28 PDT 2015


On Tuesday, 6 October 2015 at 20:44:30 UTC, via 
Digitalmars-d-learn wrote:
> On Tue, Oct 06, 2015 at 08:28:46PM +0000, Borislav Kosharov via 
> Digitalmars-d-learn wrote:
>> JSONValue root = parseJSON(text);
>> if(root["key"].isNull == false) {
>
> try
>
> if("key" in root) {
> 	// it is there
> } else {
> 	// it is not there
> }
>
>
> you can also do
>
> if("key" !in root) {}

Additionally, just like associative arrays, if you need to access 
the value, you can get a pointer to it with the in operator (and 
if the key doesn't exist, it will return a null pointer).

const(JSONValue)* p = "key" in root;
if (p)
{
     // key exists, do something with p or *p
}
else
{
     // key does not exist
}


More information about the Digitalmars-d-learn mailing list