rapidjson是腾讯开源的解析json的高效c++库,这里做个简单的封装,除去每次解析时都要先判断结点是否存在再读取的麻烦
github地址
因为是封装,所以依赖rapidjson,请自行下载rapidjson源码,使用示例如下
-
const char *JSON = "{" ""double":100.11," ""int":200," ""str":"Hello World"," ""bool":false" "}";
-
-
rapidjson::Document d;
-
-
d.Parse(JSON);
-
-
if (d.HasParseError()) { printf( "parse error!\n" ); return -1; }
-
-
uint32_t uvalue;
-
-
assert(!Wrapidjson::GetUint(d, "uintvalue", &uvalue));
-
-
assert(Wrapidjson::HasDouble(d, "double"));
-
-
double dvalue;
-
-
assert(Wrapidjson::GetDouble(d, "double", &dvalue));
-
-
assert(dvalue == 100.11);
-
-
assert(!Wrapidjson::HasDouble(d, "int"));
-
-
int ivalue;
-
-
assert(Wrapidjson::GetInt(d, "int", &ivalue));
-
-
assert(ivalue == 200);
-
-
bool bvalue;
-
-
assert(Wrapidjson::GetBool(d, "bool", &bvalue));
-
-
assert(bvalue == false);
-
-
std::string svalue;
-
-
assert(Wrapidjson::GetString(d, "str", &svalue));
-
-
assert(svalue == std::string("Hello World"));
作者:帅得不敢出门
阅读(2357) | 评论(0) | 转发(0) |