使用rapidjson生成json串
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
-
引入头文件
#include "json/document.h" #include "json/writer.h" #include "json/stringbuffer.h" using namespace rapidjson;
-
生成json串
rapidjson::Document document; document.SetObject(); rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); rapidjson::Value array(rapidjson::kArrayType); rapidjson::Value object(rapidjson::kObjectType); object.AddMember("int", 1, allocator); object.AddMember("double", 1.0, allocator); object.AddMember("bool", true, allocator); object.AddMember("hello", "HelloWorld", allocator); array.PushBack(object, allocator); //document.AddMember("json", "json string", allocator); document.AddMember("json", array, allocator); StringBuffer buffer; rapidjson::Writer<StringBuffer> writer(buffer); document.Accept(writer); CCLOG("%s", buffer.GetString()); std::string mstr = buffer.GetString(); rapidjson::Document _mDoc; _mDoc.Parse<0>(mstr.c_str()); const rapidjson::Value &pArr = _mDoc["json"]; CCLOG("Size = %d", pArr.Capacity()); for (int i = 0; i < pArr.Capacity(); ++i){ const rapidjson::Value &temp = pArr[i]; CCLOG("%d", temp["int"].GetInt()); }
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
7 个月前
更多推荐
已为社区贡献1条内容
所有评论(0)