简体   繁体   English

Rapidjson文档创建嵌套对象

[英]rapidjson document create nested object

I want to create nested json object using C++ rapdijson::document functions. 我想使用C ++ rapdijson :: document函数创建嵌套的json对象。

Here is JSON I want to write 这是我要写的JSON

{
  "a" :
  {
    "b" :
    {
      "value" : 1
    }
  }
}

Using writer I can create it like this: 使用writer我可以这样创建它:

#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
using namespace std;

void main() {
    StringBuffer s;
    Writer<StringBuffer> writer(s);

    writer.StartObject();
        writer.Key("a");
        writer.StartObject();
            writer.Key("b");
            writer.StartObject();
                writer.Key("value");
                writer.Int(1);
            writer.EndObject();
        writer.EndObject();
    writer.EndObject();
    cout << s.GetString() << endl;
}

But when I try to use document I cant create nested objects using functions like SetObject() and AddMember() only creates JSON-objects with one level. 但是,当我尝试使用文档时,无法使用SetObject()和AddMember()之类的函数创建嵌套对象,只能创建一个级别的JSON对象。

Document d;
d.SetObject().AddMember("a", "b", d.GetAllocator());

How to add nested objects into document or values created by SetObject and combine em into complex JSON file using document style? 如何将嵌套对象添加到文档或SetObject创建的值中,并使用文档样式将em组合到复杂的JSON文件中?

This code worked for me: 这段代码对我有用:

        Value valChannel;
        valChannel.SetObject();
        {
            valChannel.AddMember("sampler", c.sampler, w.mAl);

            Value valTarget;
            valTarget.SetObject();
            {
                valTarget.AddMember("id", StringRef(c.target.id->id), w.mAl);
                valTarget.AddMember("path", c.target.path, w.mAl);
            }
            valChannel.AddMember("target", valTarget, w.mAl);
        }

And last but not least add it to the document: 最后但并非最不重要的一点是将其添加到文档中:

Document d;
d.CopyFrom(valChannel, d.GetAllocator());

This code is copied from the assimp-repo: Assimp-Repo 此代码是从assimp-repo复制的: Assimp-Repo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM