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

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

但是“分配”和“网址”呢?
{
  "string1": "string",
  "string2": "string",
  "string3": "string",
  "string4": "string",
  "string5": "string",
  "allocations": [
    {
      "allocation": "string",
      "url": "string"
    }
  ]
}

最佳答案

你可以这样做

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());

关于c++ - Rapidjson 文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24084947/

10-11 22:47
查看更多