rapidjson 的json实现都是 头文件.
最近好像被 腾讯 “收” 了,自己没得创新, 腾讯老是 “玩”这种, 让人看不懂。
下面代码是 利用 rapidjson 库对 json数据进行遍历的demo。 供大家参考!!
#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include <cstdio>
#include <iostream>
using namespace rapidjson;
using namespace std;
int cc_json(Value& js_value, int d = 0){
string tmp;
char buf ;
if(js_value.IsNull() ||js_value.IsBool() ||js_value.IsNumber() ||js_value.IsString()){
for(int i = 0; i < d+1; i++){
cout << " ";
}
}
if(js_value.IsNull()){
cout << "(null)";
}else if(js_value.IsBool()){
cout << (js_value.GetBool()) ;
cout << "(bool)";
}else if(js_value.IsObject()){
for(auto iter = js_value.MemberBegin(); iter != js_value.MemberEnd(); ++iter){
auto key = (iter->name).GetString();
for(int i = 0; i < d; i++){
cout << " ";
}
cout << "- "<< key << "(object): ";
cout << endl;
cc_json(js_value[key], d+1);
if(js_value[key].IsArray()) continue;
if(iter+1 == js_value.MemberEnd()) continue;
cout <<endl;
}
}else if(js_value.IsArray()){
buf = TYPE_ARRAY_START;
for(auto i = 0; i < js_value.Size(); ++i){
cc_json(js_value[i], d);
cout <<endl;
}
buf = TYPE_OBJECT_ARRAY_END;
}else if(js_value.IsNumber()){
cout << (js_value.GetDouble()) ;
cout << "(number)";
}else if(js_value.IsString()){
cout << (js_value.GetString()) ;
cout << "(string)";
}else {
cout << "(error)";
}
return 0;
}
int main(){
std::string load_str = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4],\"abc\":{\"ded\":[\"ddd\",\"eee\"]} } ";
cout << "Original JSON:"<< endl << load_str.c_str() << endl;
rapidjson::Document readdoc;
readdoc.Parse<0>(load_str.c_str());
if(readdoc.HasParseError())
{
cout << "GetParseError" << endl;;
}
cout << "------遍历开始------" << endl;
cc_json(readdoc, 0);
cout << "------遍历结束------" <<endl;
}
程序输出:
Original JSON:
{ "hello" : "world", "t" : true , "f" : false, "n": null, "i":123, "pi": 3.1416, "a":[1, 2, 3, 4],"abc":{"ded":["ddd","eee"]} }
------遍历开始------
- hello(object):
world(string)
- t(object):
1(bool)
- f(object):
0(bool)
- n(object):
(null)
- i(object):
123(number)
- pi(object):
3.1416(number)
- a(object):
1(number)
2(number)
3(number)
4(number)
- abc(object):
- ded(object):
ddd(string)
eee(string)
------遍历结束------