Rapidjson Document

user1583007 picture user1583007 · Jun 6, 2014 · Viewed 8.1k times · Source

I am trying to create a json document using rapidjson but I don't know how I can replicate part of the following document, in particular the nested object starting with "allocations", for the others elements I do

Value valObjectString(kStringType);
valObjectString.SetString("string");
doc.AddMember("string", valObjectString, doc.GetAllocator());

But what about "allocation" and "url" ?

{
  "string1": "string",
  "string2": "string",
  "string3": "string",
  "string4": "string",
  "string5": "string",
  "allocations": [
    {
      "allocation": "string",
      "url": "string"
    }
  ]
}

Answer

Ypengju picture Ypengju · Aug 27, 2014

you can do like this

Value valObjectString(kStringType);
valObjectString.SetString("string");
doc.AddMember("string", valObjectString, doc.GetAllocator());

Value array(kArrayType);
Value tmp;
tmp.SetObject();
tmp.AddMember("allocation", "string", doc.GetAllocator());
tmp.AddMember("url", "string", doc.GetAllocator());
array.PushBack(tmp, doc.GetAllocator());
doc.AddMember("allocations", array, doc.GetAllocator());