Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Looping through JsonObject attributes is cumbersome

Moderators: kris

Posted: 04/01/09 12:38:31

Using foreach for the attributes in a JsonObject? is a bit unwieldy:

void main()
	auto json = new Json!(char);
	foreach (char[] key, Json!(char).Value val ; *json.value.toObject()) 
		Stdout("key: ")(key).newline;
		Stdout("value type: ")(val.type).newline;

Not very pretty. Makes one wonder if anyone even uses this part of Tango.

Author Message

Posted: 04/01/09 17:20:47

Yes, good point. That certainly is ugly

Posted: 04/01/09 17:44:41

with svn trunk, you can do this instead:

auto obj = json.value.toObject;

foreach (name, value; obj.attributes)
         Stdout.formatln ("{}, {}", name, value.type);

Hope that's better?

Posted: 04/06/09 11:00:09

Brilliant. =)