量化交易之C++篇 - rapidjson - demo示例

#include <iostream>

#include <fstream>
#include <string>
#include <rapidjson/document.h>

#include <boost/filesystem.hpp>

using namespace std;
using namespace rapidjson;

int main(int argc, const char *argv[]) {
    
    string filename = "test.json";
    if (boost::filesystem::exists(filename)) {
        std::cout << "File exists!" << std::endl;


        std::ifstream ifs(filename.c_str());
        std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));

        Document document;
        document.Parse(content.c_str());

        // int_key.
        if (document.HasMember("int_key")) {
            const Value& int_key = document["int_key"];
            if (int_key.IsInt()) {
                cout << "int_key: " << int_key.GetInt() << endl;
            }
        }
        cout << "---------------------------------------------------------------" << endl;

        // str_key.
        if (document.HasMember("str_key")) {
            const Value& str_key = document["str_key"];
            if (str_key.IsString()) {
                cout << "str_key: " << str_key.GetString() <<endl;
            }
        }
        cout << "---------------------------------------------------------------" << endl;
        
        // int_list_key.
        if (document.HasMember("int_list_key")) {
            const Value& int_list_key = document["int_list_key"];
            if (int_list_key.IsArray()) {
                for (Value::ConstValueIterator itr = int_list_key.Begin(); itr != int_list_key.End(); ++itr) {
                    std::cout << itr->GetInt() << std::endl;
                }
            }
        }
        cout << "---------------------------------------------------------------" << endl;

        // str_list_key.
        if (document.HasMember("str_list_key")) {
            const Value& str_list_key = document["str_list_key"];
            if (str_list_key.IsArray()) {
                for (Value::ConstValueIterator itr = str_list_key.Begin(); itr != str_list_key.End(); ++itr) {
                    std::cout << itr->GetString() << std::endl;
                }
            }
        }
        cout << "---------------------------------------------------------------" << endl;


        // bool_key.
        if (document.HasMember("bool_key")) {
            const Value& bool_key = document["bool_key"];
            if (bool_key.IsBool()) {
                cout << "bool_key: " << bool_key.GetBool() <<endl;
                if (bool_key.IsFalse()) {
                    cout << "bool_key.IsFalse()" <<endl;
                } else if (bool_key.IsTrue()) {
                    cout << "bool_key.IsTrue()" <<endl;
                }
            }
        }
        cout << "---------------------------------------------------------------" << endl;


        // null_key.
        if (document.HasMember("null_key")) {
            const Value& null_key = document["null_key"];
            if (null_key.IsNull()) {
                cout << "null_key.IsNull()" <<endl;
            }
        }
        cout << "---------------------------------------------------------------" << endl;


        // double_key.
        if (document.HasMember("double_key")) {
            const Value& double_key = document["double_key"];
            if (double_key.IsDouble()) {
                // cout << "double_key: " << double_key.GetDouble() <<endl;
                double value = double_key.GetDouble();
                printf("double_key: %.16g\n", value); // 这种方法不会损失精度
            }
        }
        cout << "---------------------------------------------------------------" << endl;


        // data_key
        if (document.HasMember("data_key")) {
            const Value& data_key = document["data_key"];
            if (data_key.IsObject()) {
                cout << "data_key yes" << endl;
            }
        }  

    } else {
        cout << "File does not exist!" << endl;
    }
    
    return 0;
}
// test.json

{
    "int_key": 10,
    "str_key": "OnePiece",
    "int_list_key": [1, 2, 3, 4, 5],
    "str_list_key": ["1_str", "2_str", "3_str", "4_str", "5_str"],
    "bool_key": false,
    "null_key": null,
    "double_key": 3.1415926,
    "data_key": {
        "sub_key": "DataCore"
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值