在工作中需要在函数里面创建一个json的dom 然后将这个数据返回到http客户端,这里分享以下怎么去创建一个dom,以及怎么去项dom中添加一般的成员和array[数组]以及object{对象}。
在我的前一篇博客中分享了如何使用rapidjson去分析一个json对象的参数,这一篇主要介绍如何去创建一个json。
代码的下载地址在这里:https://gitee.com/jeasonb/rapidjson_test 这是整个的工程
创建一个json 的第一步是声明一个dom。
然后给dom一个初始要解析的string

	rapidjson::Document document;
    document.Parse("{\"key\":1,\"key2\":[1,2,3,4]}");
    //document.Parse("{}");  这两种方法都可以 如果有已知的可以上面的方法加入成员

接下来要声明一个allocator ,具体的原理我不是很明白,猜测就是一个关于这个json的指针,指示接下来的dom 去申请内存用的 ,总之在一个json文件里添加任何的变量都需要用到这个allocator!!!
可能是用于索引吧。

    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();

添加一般的成员变量比较简单,这里要注意关于string 的引入 。我在实际开发的时候遇到了不加修饰的
char[] 报错问题
‘rapidjson::GenericStringRef::GenericStringRef(CharType (&)[N]) [with unsigned int N = 20; CharType = char]’ is private within this context

	char str[20] = {'s','t','r','i','n','g',};
    document.AddMember("string","this is string!",allocator);
    document.AddMember("string2",rapidjson::StringRef(str),allocator);//可以用宏定义修饰 解决一些报错
    document.AddMember("int",123,allocator);
    document.AddMember("double",123.45678,allocator);
    document.AddMember("bool",true,allocator);

接下来就是本次分享的重头戏,嵌套的arry和object.
json的结构在我看来是一种可以无限嵌套的结构,在Array 中的成员可以是object,而object 有能包含很多的object以及arrary。
先做一个array, 在写arrary 之前我们需要先去声明一个arrary的 Value
然后将数据像压堆栈一样依次的push进去。先测试以下简单的类型的arrary

    rapidjson::Value a(rapidjson::Type::kArrayType);
    for(int i=0;i<5;i++)
    {
        a.PushBack(i*2,allocator);
    }
    document.AddMember("Int_arrary",a,allocator);   

不是很清楚具体的实现机制,但是看起来就像是压入堆栈一样。
截至目前我们实现的dom的输出是这样的

{
    "key": 1,
    "key2": [
        1,
        2,
        3,
        4
    ],
    "string": "this is string!",
    "string2": "string",
    "int": 123,
    "double": 123.45678,
    "bool": true,
    "Int_arrary": [
        0,
        2,
        4,
        6,
        8
    ],
  ........

接下来我们将object加入到我们的dom 中。

 rapidjson::Value b(rapidjson::Type::kObjectType);    
    b.AddMember("city"    ,"shenzhen",allocator);
    b.AddMember("author"  ,"jeason",allocator);
    b.AddMember("email","1666532400@qq.com",allocator);
    b.AddMember("age"  ,11111,allocator);
    
    // object 内部嵌套arrary 
    rapidjson::Value c(rapidjson::Type::kArrayType);
    for(int i=0;i<5;i++)
    {
        c.PushBack(i*3,allocator);
    }
    b.AddMember("arrary_in_object",c,allocator);
    document.AddMember("obj",b,allocator);

上面的代码实现了向一个object 内部添加成员,并且其中的一个成员是一个arrary
输出的内容:


    "obj": {
        "city": "shenzhen",
        "author": "jeason",
        "email": "1666532400@qq.com",
        "age": 11111,
        "arrary_in_object": [
            0,
            3,
            6,
            9,
            12
        ]
    },

接下来我们创建一个 全部是object的arrary

 rapidjson::Value array_obj(rapidjson::Type::kArrayType);
    for(int i=0;i<3;i++)
    {
        rapidjson::Value temp(rapidjson::Type::kObjectType);
        temp.AddMember("test",i,allocator);
        temp.AddMember("test*2",i*2,allocator);
        array_obj.PushBack(temp,allocator);
    }
    document.AddMember("array_obj",array_obj,allocator);

输出的结果

"array_obj": [
        {
            "test": 0,
            "test*2": 0
        },
        {
            "test": 1,
            "test*2": 2
        },
        {
            "test": 2,
            "test*2": 4
        }
    ]

从上面的结构可以推断出,arrary和object 是可以无限套娃的,更深层次的套娃我就不去介绍了,读者们自行研究吧。
代码的下载地址在这里:https://gitee.com/jeasonb/rapidjson_test 这是整个的工程
开发环境是windows10 vscode + cmake + mingw32
具体的参数配置读者自行研究或者给我留言吧!

#include <stdio.h>
#include <iostream>
#include "./rapidjson/include/document.h"
#include "./rapidjson/include/stringbuffer.h"
#include "./rapidjson/include/istreamwrapper.h"
#include "./rapidjson/include/prettywriter.h"
#include "./rapidjson/include/filereadstream.h"
#include "./rapidjson/include/filewritestream.h"
#include "./rapidjson/include/pointer.h"
#include "./rapidjson/include/writer.h"
#include <fstream>
using namespace std;

void test(void);
int main(void)
{
    test();
    system("pause");
    return 0;
}
void test(void)
{
	rapidjson::Document document;
    document.Parse("{\"key\":1,\"key2\":[1,2,3,4]}");   
    //document.Parse("{}");  这两种方法都可以 如果有已知的可以上面的方法加入成员
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
    
    char str[20] = {'s','t','r','i','n','g',};
    document.AddMember("string","this is string!",allocator);
    document.AddMember("string2",rapidjson::StringRef(str),allocator);//可以用宏定义修饰 解决一些报错
    document.AddMember("int",123,allocator);
    document.AddMember("double",123.45678,allocator);
    document.AddMember("bool",true,allocator);
    
    //if(document.HasMember("key"))
    rapidjson::Value a(rapidjson::Type::kArrayType);
    for(int i=0;i<5;i++)
    {
        a.PushBack(i*2,allocator);
    }
    document.AddMember("Int_arrary",a,allocator);
   
    rapidjson::Value b(rapidjson::Type::kObjectType);    
    b.AddMember("city"    ,"shenzhen",allocator);
    b.AddMember("author"  ,"jeason",allocator);
    b.AddMember("email","1666532400@qq.com",allocator);
    b.AddMember("age"  ,11111,allocator);
    
    // object 内部嵌套arrary 
    rapidjson::Value c(rapidjson::Type::kArrayType);
    for(int i=0;i<5;i++)
    {
        c.PushBack(i*3,allocator);
    }
    b.AddMember("arrary_in_object",c,allocator);
    document.AddMember("obj",b,allocator);
    
    rapidjson::Value array_obj(rapidjson::Type::kArrayType);
    for(int i=0;i<3;i++)
    {
        rapidjson::Value temp(rapidjson::Type::kObjectType);
        temp.AddMember("test",i,allocator);
        temp.AddMember("test*2",i*2,allocator);
        array_obj.PushBack(temp,allocator);
    }
    document.AddMember("array_obj",array_obj,allocator);
  
    rapidjson::StringBuffer buffer ;  
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);  
  
    document.Accept(writer);  
    printf("buffer : %s",buffer.GetString());
        
    std::string temp(buffer.GetString());
}

完整的输出:

buffer : {
    "key": 1,
    "key2": [
        1,
        2,
        3,
        4
    ],
    "string": "this is string!",
    "string2": "string",
    "int": 123,
    "double": 123.45678,
    "bool": true,
    "Int_arrary": [
        0,
        2,
        4,
        6,
        8
    ],
    "obj": {
        "city": "shenzhen",
        "author": "jeason",
        "email": "1666532400@qq.com",
        "age": 11111,
        "arrary_in_object": [
            0,
            3,
            6,
            9,
            12
        ]
    },
    "array_obj": [
        {
            "test": 0,
            "test*2": 0
        },
        {
            "test": 1,
            "test*2": 2
        },
        {
            "test": 2,
            "test*2": 4
        }
    ]
}请按任意键继续. . .
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:9 天前 )
960b763e 2 个月前
8c391e04 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐