building a simple json tree

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 15 03:16:56 PST 2015


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]);
     }
}
---


More information about the Digitalmars-d-learn mailing list