Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rapidjson Document

Tags:

c++

rapidjson

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"
    }
  ]
}
like image 548
user1583007 Avatar asked Jan 10 '23 11:01

user1583007


1 Answers

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());
like image 158
ypengju Avatar answered Jan 21 '23 14:01

ypengju