RejectedSoftware Forums

Sign up

Looping over JSON elements

Hello,

How would I loop over the keys of a JSON object since it appears to be represented as JSON[string]? Or, perhaps the better question is how do I access the keys? Treating the JSON object as an associative array does not work for me.

Re: Looping over JSON elements

Am 21.04.2014 20:17, schrieb Casey:

Hello,

How would I loop over the keys of a JSON object since it appears to be represented as JSON[string]? Or, perhaps the better question is how do I access the keys? Treating the JSON object as an associative array does not work for me.

Using either of the following should work:

// works for objects
foreach (string key, value; json) {}

// works for arrays and objects
foreach (value; json) {}

// works for arrays
foreach (size_t index, value; json) {}

An explicit type for the key is necessary to disambiguate the array and
object case.

Re: Looping over JSON elements

I must have screwed something up earlier and not realized it because I swear I tried the same thing earlier and it didn't work. Now, it works perfectly.

Thanks!