rapidjson 使用

array 数组

#include <iostream>
#include <rapidjson.h>
#include <prettywriter.h>
#include <filereadstream.h>
#include <stringbuffer.h>
#include <document.h>
using namespace std;
using namespace rapidjson;
int main(int argc, const char * argv[]) {
    Document document;
    Document::AllocatorType &allocator = document.GetAllocator();
    Value contact1(kArrayType) ;
    Value contact2(kArrayType) ;
    Value root(kArrayType);
    contact1.PushBack("7,3,1", allocator).PushBack("0", allocator).PushBack("1", allocator);
    contact2.PushBack("7,3,9", allocator).PushBack("1", allocator).PushBack("0", allocator);
    root.PushBack(contact1, allocator);
    root.PushBack(contact2, allocator);
    
    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    root.Accept(writer);
    string reststr = buffer.GetString();
    cout << reststr << endl;

    Document doc;
    if(doc.Parse(reststr.c_str()).HasParseError())
    {
        cout << "error" << endl;
    }
    Document::ValueIterator it = doc.Begin();
    Document::ValueIterator ed = doc.End();
    while(it != ed )
    {//解析数组
        int len = it->Capacity();
        cout << (*it)[1].GetInt() << endl;
        for(int i = 0; i < len; ++i)
        {
            cout << (*it)[i].GetString() << endl;
        }
        ++it;
    }
    return 0;
}

对象

#include <iostream>
#include <rapidjson.h>
#include <prettywriter.h>
#include <filereadstream.h>
#include <stringbuffer.h>
#include <document.h>
using namespace std;
using namespace rapidjson;
int main(int argc, const char * argv[]) {
  
    Document document;
    Document::AllocatorType& allocator = document.GetAllocator();
    
    Value root(kArrayType);
    Value items(kObjectType);
    Value val(kStringType);
    
    val.SetInt(5);
    
    items.AddMember("test", val, allocator);
    val.SetInt(10);
    items.AddMember("world", val, allocator);
    root.PushBack(items, allocator);
    
    items.SetObject();
    val.SetInt(5);
    items.AddMember("test", val, allocator);
    val.SetInt(10);
    items.AddMember("world", val, allocator);
    root.PushBack(items, allocator);
    
    StringBuffer buffer;
    
    Writer<StringBuffer> writer(buffer);
    root.Accept(writer);
    std::string result = buffer.GetString();
    
    
    Document rd;
    if(rd.Parse(result.c_str()).HasParseError())
    {
        cout << "error!" << endl;
    }
    
    Document::ValueIterator it = rd.Begin();
    Document::ValueIterator ed = rd.End();
    
    while(it != ed)
    {
        Document::MemberIterator iit = it->MemberBegin();
        Document::MemberIterator eed = it->MemberEnd();
        while(iit != eed )
        {
            cout << iit->name.GetString() << " " << iit->value.GetInt()  << endl;
            ++iit;
        }
        ++it;
    }
    
    
    return 0;
}



高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API 简介 RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自RapidXml 。 RapidJSON 小而全。它同时支持 SAX 和 DOM 风格的 API。SAX 解析器只有约 500 行代码。 RapidJSON 快。它的性能可与 strlen() 相比。可支持 SSE2/SSE4.2 加速。 RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至不依赖于 STL。 RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 RapidJSON 对 Unicode 友好。它支持 UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个 UTF-8 文件至 DOM 时,把当中的 JSON 字符串转码至 UTF-16。它也支持代理对(surrogate pair)及 "\u0000"(空字符)。 JSON(JavaScript Object Notation)是一个轻量的xx交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404,并支持可选的放宽语法。 一、使用说明         rapidjson.fne 为英文原版         rapidjson_cn.fne  为中文翻译版(翻译的中文命令有点糙),使用中文版时,请改文件名为rapidjson.fne 不然会出错的。         rapidjson_static.lib 为静态库,部分中文英文。         中文和英文可以无缝切换,直接替换支持库文件就可以了。         本支持库由VS2017,所以编译时,也必须要用VS2017编译。         VC2017连接器下载地址: http://bbs.eyuyan.com/read.php?tid=410252   VS2017易支持库模板+VC2017链接器(讨厌vc6的可看)-->hxznhf http://bbs.eyuyan.com/read.php?tid=408541   全易论坛独创首发,Vs2017Linker编译器 -->cs666         关于RapidJSON的相关问题,请看:http://rapidjson.org/zh-cn/md_doc_faq_8zh-cn.html         使用有声明问题请加QQ群: 心宇->EVAxx研究中心(255829517)         现在支持库完善了大概80%左右,已经可以正常使用,剩下20%是关于 reader 和编码以及一些参数有关,暂时对易不是很重要,后面也会完善。 二、版权声明         本支持库为封装TX开源项目 RapidJSON ,为解决易语言 没有高性能JSON库的问题。         项目官网:http://rapidjson.org/zh-cn/         github:https://github.com/Tencent/rapidjson/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值