1. 引入头文件

    #include "json/document.h"
    #include "json/writer.h"
    #include "json/stringbuffer.h"
    using namespace  rapidjson;
    
  2. 生成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 天前
Logo

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

更多推荐