rapidjson的学习及使用
2021-04-13 11:39
标签:link dom 序列化 http c_str kobject bre serial doc 首先,附上官方链接: 使用rapidjson,主要是对json串进行解析和拼接,即反序列化(Deserialize)和序列化(serialize) 反序列如下示例: void DeserializeFunc(string json) Value& val = obj["Photos"]; //序列化示例 list Document doc; //序列化 while(it != idsList.end()) doc.AddMember("ids", ids, allocator); Writer } //对结果进行序列化处理,并返回处理后的json串 doc.AddMember("error_code", ErrCode, allocator); rapidjson的学习及使用 标签:link dom 序列化 http c_str kobject bre serial doc 原文地址:https://www.cnblogs.com/QQ-354296528/p/8976486.html
http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html
{
//映射DOM
doc.Parse(json.c_str());
if (doc.HasParseError())
{
return;
}
assert(doc.IsArray());//断言doc是不是数组格式数据
for(size_t i = 0; i {
Value& obj = doc[i];
if(obj.HasMember("Temperature") && obj["Temperature"].IsObject())//格式为对象时
{
Value& temp = obj["Temperature"];
if(temp.HasMember("Metric") && temp["Metric"].IsObject())
{
Value& currTemp = temp["Metric"];
if(currTemp.HasMember("Value") && currTemp["Value"].IsDouble())//格式为double时
{
double currVal = currTemp["Value"].GetDouble();
currentInfo.temperature = currTemp["Value"].GetDouble();
}
}
}
if(obj.HasMember("Photos") && obj["Photos"].IsArray())//格式为数组时
{
for(size_t i = 0; i {
Value& photo = val[i];
if(photo.HasMember("LandscapeLink") && photo["LandscapeLink"].IsString())//格式为string时
{
string urlLink = photo["LandscapeLink"].GetString();
currentInfo.bgURL = photo["LandscapeLink"].GetString();
if(urlLink.length() > 0 && urlLink.find("_L_L") != string::npos )
{
break;
}
}
}
}
}
}
string serialize_data(list
{
if(idsList.size() == 0)
{
return "";
}
doc.SetObject();
Document::AllocatorType &allocator = doc.GetAllocator();
StringBuffer buffer;
rapidjson::Value ids(kArrayType);
{
ids.PushBack(rapidjson::GenericStringRef
it++;
}
doc.Accept(writer);
return buffer.GetString();
string deal_result_message(int ErrCode)
{
Document doc; //序列化
doc.SetObject();
Document::AllocatorType &allocator = doc.GetAllocator();
StringBuffer buffer;
doc.AddMember("error_msg", rapidjson::GenericStringRef
rapidjson::Value data(kObjectType);
doc.AddMember("data", data, allocator);
Writer
doc.Accept(writer);
const char* sJson = buffer.GetString();
return sJson;
}