Json11开源库使用

源码下载:GitHub - dropbox/json11: A tiny JSON library for C++11.

1.加载json11库

它用起来非常方便,只有两个文件,一个json11.hpp,一个json11.cpp。

2.代码示例
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "json11.hpp"

#ifdef _MSC_VER
#include <Windows.h>
static std::string utf8_to_gbk(const char *utf8)
{
    char gbk[256];
    const int maxlen = 256;
    wchar_t unicode_str[maxlen];
    int outlen = MultiByteToWideChar(CP_UTF8, 0, utf8, strlen(utf8), unicode_str, maxlen);
    outlen = WideCharToMultiByte(CP_ACP, 0, unicode_str, outlen, gbk, 256, NULL, NULL);
    gbk[outlen] = '\0';

    std::string str;
    str.assign(gbk);
    return str;
}
#endif

int test1();
int test2();
int test3();
int test4();

int main()
{
    test4();

    std::cout << "ok" << std::endl;
    return 0;
}

int test4()
{
    // std::istringstream
    std::filebuf in;
    if (!in.open("E:/GitCode/Messy_Test/testdata/json.data", std::ios::in))
    {
        std::cout << "fail to open file" << std::endl;
        return -1;
    }

    std::istream iss(&in);

    std::istreambuf_iterator<char> eos;
    std::string s(std::istreambuf_iterator<char>(iss), eos);
    std::string err;
    auto json = json11::Json::parse(s, err);
    if (!err.empty())
    {
        in.close();
        return -1;
    }

    std::cout << "************** show file info *****************" << std::endl;
    std::string json_str = json.dump();
    std::cout << json_str << std::endl;

    std::cout << "*************** start parse ****************" << std::endl;

    std::string name = json["name"].string_value();
    std::string addr = json["address"].string_value();
    int age = json["age"].int_value();

    json11::Json::array array_ = json["value1"].array_items();
    std::vector<std::vector<float>> value1;
    value1.resize(array_.size());
    for (int i = 0; i < array_.size(); i++)
    {
        json11::Json::array tmp = array_[i].array_items();

        value1[i].resize(tmp.size());
        for (int j = 0; j < tmp.size(); j++)
        {
            value1[i][j] = tmp[j].number_value();
        }
    }

    std::vector<float> value2;
    array_ = json["value2"].array_items();
    value2.resize(array_.size());
    for (int i = 0; i < array_.size(); i++)
    {
        value2[i] = array_[i].number_value();
    }

    std::string beijing_addr;
    bool beijing_car, beijing_cat;
    if (json["bei_jing"].is_null())
    {
        std::cout << "failed to get bei_jing info" << std::endl;
    }
    else
    {
        json11::Json::object object_ = json["bei_jing"].object_items();

        beijing_addr = object_["address"].string_value();
        beijing_car = object_["car"].bool_value();
        beijing_cat = object_["cat"].bool_value();
    }

    std::string shandong_addr, tmp_addr;
    std::vector<std::string> shandong_value1;
    if (json["shan_dong"].is_null())
    {
        std::cout << "failed to get shan_dong info" << std::endl;
    }
    else
    {
        json11::Json::object object_ = json["shan_dong"].object_items();

        tmp_addr = object_["tmp"].string_value();
        if (tmp_addr == "")
        {
            std::cout << "tmp is null" << std::endl;
        }

        shandong_addr = object_["address"].string_value();

        json11::Json::array tmp = object_["value1"].array_items();
        shandong_value1.resize(array_.size());
        json11::Json json_ = tmp[0];
        shandong_value1[0] = json_["ji_nan"].string_value();
        json_ = tmp[1];
        shandong_value1[1] = json_["tai_an"].string_value();
    }

    std::cout << "************** parse result ***************" << std::endl;
    std::cout << "name: " << utf8_to_gbk(name.c_str()) << std::endl;
    std::cout << "address: " << utf8_to_gbk(addr.c_str()) << std::endl;
    std::cout << "age: " << age << std::endl;

    std::cout << "value1: " << std::endl;
    for (int i = 0; i < value1.size(); i++)
    {
        for (int j = 0; j < value1[i].size(); j++)
        {
            std::cout << "  " << value1[i][j];
        }
        std::cout << std::endl;
    }

    std::cout << "value2: " << std::endl;
    for (int i = 0; i < value2.size(); i++)
    {
        std::cout << "  " << value2[i];
    }
    std::cout << std::endl;

    std::cout << "bei_jing info: " << std::endl;
    std::cout << "  address: " << utf8_to_gbk(beijing_addr.c_str()) << std::endl;
    std::cout << "  car: " << beijing_car << std::endl;
    std::cout << "  cat: " << beijing_cat << std::endl;

    std::cout << "shan_dong info: " << std::endl;
    std::cout << "  address: " << utf8_to_gbk(shandong_addr.c_str()) << std::endl;
    std::cout << "  value1: " << std::endl;
    std::cout << "    ji_nan: " << utf8_to_gbk(shandong_value1[0].c_str()) << std::endl;
    std::cout << "    tai_an: " << utf8_to_gbk(shandong_value1[1].c_str()) << std::endl;

    in.close();
    return 0;
}

int test1()
{
    json11::Json my_json = json11::Json::object{
        {"中国", "北京"},
        {"key2", false},
        {"key3", json11::Json::array{1, 2, 3}},
    };
    std::string json_str = my_json.dump();
    std::string key_1 = my_json["中国"].string_value();

    std::cout << json_str << std::endl;
    std::cout << key_1 << std::endl;

    return 0;
}

int test2()
{
    class Point
    {
    public:
        int x;
        int y;
        Point(int x, int y) : x(x), y(y) {}
        json11::Json to_json() const { return json11::Json::array{x, y}; }
    };

    std::vector<Point> points = {{1, 2}, {10, 20}, {100, 200}};
    std::string points_json = json11::Json(points).dump();

    std::cout << points_json << std::endl;

    return 0;
}

int test3()
{
    json11::Json json = json11::Json::array{json11::Json::object{{"k", "v"}}};
    std::string str = json[0]["k"].string_value();

    std::cout << str << std::endl;

    return 0;
}

json文件内容如下所示:

cpp 复制代码
{
    "name": "spring",
    "address": "北京",
    "age": 30,
    "value1": [
        [
            23,
            43,
            -2.3,
            6.7,
            90
        ],
        [
            -9,
            -19,
            10,
            2
        ],
        [
            -5,
            -55
        ]
    ],
    "value2": [
        13.3,
        1.9,
        2.10
    ],
    "bei_jing": {
        "address": "海淀",
        "car": false,
        "cat": true
    },
    "shan_dong": {
        "address": "济南",
        "value1": [
            {
                "ji_nan": "趵突泉"
            },
            {
                "tai_an": "泰山"
            }
        ]
    }
}
相关推荐
逆天的蝈蝈21 分钟前
开源与商业的碰撞TPFLOW与Gadmin低代码的商业合作
低代码·开源
羊小猪~~1 小时前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
苏三有春2 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
脉牛杂德2 小时前
多项式加法——C语言
数据结构·c++·算法
legend_jz2 小时前
STL--哈希
c++·算法·哈希算法
CSUC2 小时前
【C++】父类参数有默认值时子类构造函数列表中可以省略该参数
c++
Vanranrr2 小时前
C++ QT
java·c++·qt
鸿儒5172 小时前
C++ lambda 匿名函数
开发语言·c++
兆。2 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
FIT2CLOUD飞致云2 小时前
仪表板展示|DataEase看中国:历年双十一电商销售数据分析
数据分析·开源·数据可视化·dataease·双十一