JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)

科技资讯 投稿 6200 0 评论

JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)

JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)

介绍

想知道更多有关JSON格式的介绍,可以到JSON的官网json.org学习

Github地址:jsoncpp

使用实例

因为它有一个非常好的特性:下标访问(包括array类型)

    const std::string name = root["Name"].asString(;
    const int age = root["Age"].asInt(;

上述代码中,Name字段在JSON中是一个String类型,然后通过解析后的root(Json::Value对象)的下标访问

读取解析读取string

读取一个JSON格式的string,代码如下

#include "json/json.h"
#include <iostream>
#include <memory>

int readJsonExample( {
    const std::string rawJson = R"({"Age": 20, "Name": "colin"}";
    const auto rawJsonLength = static_cast<int>(rawJson.length(;
    constexpr bool shouldUseOldWay = false;
    JSONCPP_STRING err;
    Json::Value root;

    if (shouldUseOldWay {
        Json::Reader reader;
        reader.parse(rawJson, root;
    } else {
        Json::CharReaderBuilder builder;
        const std::unique_ptr<Json::CharReader> reader(builder.newCharReader(;
        if (!reader->parse(rawJson.c_str(, rawJson.c_str( + rawJsonLength, &root,
                           &err {
            std::cout << "error" << std::endl;
            return EXIT_FAILURE;
        }
    }
    const std::string name = root["Name"].asString(;
    const int age = root["Age"].asInt(;

    std::cout << name << std::endl;
    std::cout << age << std::endl;
    return EXIT_SUCCESS;
}
int main( {
    return readJsonExample(;
}

可以看到上面没有使用oldway去解析,新的方法采用CharReaderBuilder生成Reader,其实新的解析写法和之前没有很大的不一样

colin
20

从原始数据构建JSON并序列化

#include "json/json.h"
#include <iostream>

int writeJsonString( {
    Json::Value root;
    Json::Value data;
    constexpr bool shouldUseOldWay = false;
    root["action"] = "run";
    data["number"] = 1;
    root["data"] = data;

    if (shouldUseOldWay {
        Json::FastWriter writer;
        const std::string json_file = writer.write(root;
        std::cout << json_file << std::endl;
    } else {
        Json::StreamWriterBuilder builder;
        const std::string json_file = Json::writeString(builder, root;
        std::cout << json_file << std::endl;
    }
    return EXIT_SUCCESS;
}
int main( {
    return writeJsonString(;
}

可以看到差不多,其实可以直接理解为访问对象的下标,然后把实际的数值放进去,JsonCpp会自动判断等号右边的类型。很直观,也比较容易理解

{
        "action" : "run",
        "data" :
        {
                "number" : 1
        }
}

以库的形式添加到项目中

本文为作者原创文章,引用请注明出处:https://www.cnblogs.com/nbtech/p/use_jsoncpp_library.html

mkdir UseJsonCppProject && cd UseJsonCppProject
git clone https://github.com/open-source-parsers/jsoncpp.git
vim CMakeLists.txt

CMakeLists.txt内容如下

cmake_minimum_required(VERSION 3.0 FATAL_ERROR
project(jsoncpp-test LANGUAGES CXX

# jsoncpp
include_directories(jsoncpp/include
add_subdirectory(jsoncpp lib

# executable output
add_executable(jt main.cpp
target_link_libraries(jt jsoncpp_lib

这里解释一下前面两行声明项目,include_directories是包含jsoncpp的头文件,add_subdirectory是添加jsoncpp工程本身的CMakeLists.txt到当前目录下,最后两行是添加可执行文件还有将jsoncpp库链接到可执行文件

main.cpp文件可以使用上面的示例

# https://www.cnblogs.com/nbtech/p/use_jsoncpp_library.html
mkdir build && cd build
cmake .. && make

在我们项目中,用CMake添加jsoncpp库操作如上

交叉编译?

有时候我们希望它可以跨平台,那么只需要在cmake配置的时候指定交叉编译工具即可

cmake -D CMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ ..

编程笔记 » JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)

赞同 (21) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽