首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

rapidjson常见使用示例

Document d; v2.CopyFrom(d, a); // 把整个document复制至v2,d不变 rapidjson为了最大化性能,大量使用了浅拷贝,使用之前一定要了解清楚。 如果采用了浅拷贝,特别要注意局部对象的使用,以防止对象已被析构了,却还在被使用。 // 需要#include的头文件: #include #include // en为english的简写,定义了取出错信息的函数GetParseError_En(errcode) #include #include // 示例1:解析一个字符串 // 运行输出结果: // count=2 // name=zhangsan // name=wangwu void x1() {     rapidjson::Document document; // 定义一个Document对象     std::string str = "{\"count\":2,\"names\":[\"zhangsan\",\"wangwu\"]}";     document.Parse(str.c_str()); // 解析,Parse()无返回值,也不会抛异常     if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功     {         // 可通过GetParseError()取得出错代码,         // 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值         // 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写         // 函数GetErrorOffset()返回出错发生的位置         printf("parse error: (%d:%d)%s\n", document.GetParseError(), document.GetErrorOffset(), rapidjson::GetParseError_En(document.GetParseError()));     }     else     {         // 判断某成员是否存在         if (!document.HasMember("count") || !document.HasMember("names"))         {             printf("invalid format: %s\n", str.c_str());         }         else         {             // 如果count不存在,则运行程序会挂,DEBUG模式下直接abort             rapidjson::Value& count_json = document["count"];             // 如果count不是整数类型,调用也会挂,DEBUG模式下直接abort             // GetInt()返回类型为int             // GetUint()返回类型为unsigned int             // GetInt64()返回类型为int64_t             // GetUint64()返回类型为uint64_t             // GetDouble()返回类型为double             // GetString()返回类型为char*             // GetBool()返回类型为bool             int count = count_json.GetInt();             printf("count=%d\n", count);             // 方法GetType()返回枚举值: kNullType,kFalseType,kTrueType,kObjectType,kArrayType,kStringType,kNumberType             // 可用IsArray()判断是否为数组,示例: { "a": [1, 2, 3, 4] }             // 用IsString()判断是否为字符串值             // 用IsDouble()判断是否为double类型的值,示例: { "pi": 3.1416 }             // 用IsInt()判

03
领券