what's the right syntax for building a JSON tree ? I try to do like in an AA but the program throw because the Key doesn't exist:

---
import std.stdio, std.json;

void main(string[] args)
{
    struct Foo{
        string a,  b;
        void writeToJson(ref JSONValue target) {
            target["a"] = JSONValue(a);
            target["b"] = JSONValue(b);
        }
    }

    JSONValue root = parseJSON("{}");
    root["items"] = JSONValue([""]);

    Foo*[] foos;
    foos ~= new Foo("a1","b1");
    foos ~= new Foo("a2","b2");

    foreach(foo; foos) {
        root["items"].array.length += 1;
        root["items"].array[$-1] = parseJSON("{}");
        foo.writeToJson(root["items"].array[$-1]);
    }
}
---

Reply via email to