使用 rapidjson 对 json 数据进行遍历

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)
------遍历结束------

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值