如果一個層級為data,其内部又包含一個層級status, 可以直接通過這種方式來跨級通路:
rapidjson::Value& val_status = doc["data"]["status"];
#include "json/rapidjson.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
rapidjson::Document doc;
doc.Parse<0>(data.bytes);
if (doc.HasParseError())
{
//CCLOG("%s ---> rapidjson parse error!!! %s\n", __FUNCTION__, doc.GetParseError());
return;
}
else
{
rapidjson::Value& val_status = doc["data"]["status"];
if (val_status.IsInt())
{
log("====== val_data_status: %d", val_status.GetInt
}
}
層級太多,提現提取用的比較多的某一層級,減少不必要的上層節點的重複擷取,這裡節點L需要被多次通路,是以提前把L提取出來,友善之後L内部對象的提取,運算效率上沒想太多,應該沒有啥差別,rapidjson内部不會傻到用數組周遊
rapidjson::Value& val_L = doc["data"]["result"]["L"];
rapidjson::Value& val_index1 = val_L["index1"];
rapidjson::Value& val_index2 = val_L["index2"];
int index1 = val_index1.IsInt() ? val_index1.GetInt() : 0;
int index2 = val_index2.IsInt() ? val_index2.GetInt() : 0;
轉載于:https://www.cnblogs.com/JD85/p/10060080.html