Rapidjson文件

use*_*007 4 c++ rapidjson

我正在尝试使用rapidjson创建一个json文档,但我不知道如何复制以下文档的一部分,特别是以"allocations"开头的嵌套对象,对于我做的其他元素

Value valObjectString(kStringType);
valObjectString.SetString("string");
doc.AddMember("string", valObjectString, doc.GetAllocator());
Run Code Online (Sandbox Code Playgroud)

但是"分配"和"网址"呢?

{
  "string1": "string",
  "string2": "string",
  "string3": "string",
  "string4": "string",
  "string5": "string",
  "allocations": [
    {
      "allocation": "string",
      "url": "string"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

小智 7

你可以这样做

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());
Run Code Online (Sandbox Code Playgroud)