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
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 7 个月前
Logo

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

更多推荐