使用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
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
2d42229f
* Support BSON uint64 de/serialization
Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>
* Treat 0x11 as uint64 and not timestamp specific
Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>
---------
Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com> 4 天前
1809b3d8
Signed-off-by: Niels Lohmann <mail@nlohmann.me> 5 天前
更多推荐
已为社区贡献1条内容
所有评论(0)