菜鸟笔记
提升您的技术认知

rapidjson 构造数组

构造示例:

{
    "userList": [
        {
            "userId" : 20,
            "productIdList" : [1, 2],
        },
        {
            "userId" : 21,
            "productIdList" : [1, 2]
        }
    ]
}

代码示例:

#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;
using namespace std;

int main() {
    StringBuffer s;
    Writer<StringBuffer> writer(s);
    writer.StartObject();

    writer.Key("userList");
    writer.StartArray();

    writer.StartObject();
    writer.Key("userId");
    writer.Uint(20);
    writer.Key("productIdList");
    writer.StartArray();
    for (unsigned i = 1; i < 3; i++)
        writer.Uint(i);
    writer.EndArray();
    writer.EndObject();

    writer.StartObject();
    writer.Key("userId");
    writer.Uint(21);
    writer.Key("productIdList");
    writer.StartArray();
    for (unsigned i = 1; i < 3; i++)
        writer.Uint(i);
    writer.EndArray();
    writer.EndObject();

    writer.EndArray();
    writer.EndObject();
    cout << s.GetString() << endl;
}