//引入以下头文件
#include <external/json/document.h>
#include <external/json/writer.h>
#include <external/json/stringbuffer.h>
// 把数据写入json串
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value array(rapidjson::kArrayType);
for (int i=0; i <3; i++) {
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("tig", i, allocator);
object.AddMember("name", "阿龙", allocator);
object.AddMember("age", "990岁", allocator);
object.AddMember("dead", false, allocator);
array.PushBack(object, allocator);
}
document.AddMember("propety", "PLAYER-TO", allocator);
document.AddMember("player", array, allocator);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
CCLOG("buffer==%s", buffer.GetString());
/**
解析josn串
数据取时一定要对应类型 如:int bool stirng
也可以判断类型 如 bool isint = arrayaaa[i]["id"].IsInt();
*/
rapidjson::Document d;
d.Parse<0>(buffer.GetString());
if (d.HasParseError()) {
// 解析失败
}else{
// 解析成功
CCLOG("pro==%s",d["propety"].GetString());
rapidjson::Value &arrayaaa = d["player"];
CCLOG("root.Size()==%d",arrayaaa.Size());
for (int i = 0; i < arrayaaa.Size(); i++) {
CCLOG("id===%d",arrayaaa[i]["tig"].GetInt());
CCLOG("id===%s",arrayaaa[i]["name"].GetString());
CCLOG("id===%s",arrayaaa[i]["age"].GetString());
CCLOG("id===%d",arrayaaa[i]["dead"].GetBool());
}
}