jsoncpp+cmake使用

写在前面

1、本文内容
jsoncpp编译及其使用

2、平台
windows10, linux
3、转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/129300456

准备

clone源码

mkdir json
cd json
git clone https://github.com/open-source-parsers/jsoncpp.git

或者直接下载:https://codeload.github.com/open-source-parsers/jsoncpp/zip/refs/heads/master
解压到json文件夹下

编译

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="D:/carlos/install/jsoncpp" -S ./jsoncpp-master -B ./build
cmake --build ./build --config Release --target install

DCMAKE_INSTALL_PREFIX=D:/carlos/install/jsoncpp是指定编译完成后的安装路径,根据需要自行修改

使用

mkdir jsontest
cd jsontest
mkdir src

将cpp和CMakeLists.txt放入jsontest/src文件夹
把compile.bat和run.bat放进jsontest文件夹
在这里插入图片描述

jsontest.cpp

#include <iostream>
#include <fstream>

#include <json/json.h>

bool writeMapInfo(std::string file_path, Json::Value &root)
{
    
    

    std::cout << "\n\nwrite MapInfo to json file" << std::endl;

    Json::Value info;
    std::string name;
    int age;

    // 1
    name = "abc";
    age = 24;
    info["name"] = name;
    info["age"] = age;
    info["arr"][0] = 5.5;
    info["arr"][3] = 76.1;
    info["arr"][1] = 9.2;

    root["p1"] = info;

    // 2
    name = "def";
    age = 55;
    info["name"] = name;
    info["age"] = age;
    info["arr"][0] = 55.6;
    info["arr"][2] = 15.3;
    info["arr"][1] = 3.6;

    root["p2"] = info;

    Json::StyledWriter style_writer;
    std::string str = style_writer.write(root);

    std::ofstream file_out;
    file_out.open(file_path);
    if (!file_out.is_open())
    {
    
    
        std::cout << "open file" << file_path <<" failed!" << std::endl;
        return false;
    }
    {
    
    
        file_out << str;
        file_out.close();
    }
    return true;
}

bool loadMapInfo(std::string file_path, Json::Value &root)
{
    
    
    std::cout << "\n\nload MapInfo from json file" << std::endl;

    std::ifstream file_in;
    file_in.open(file_path);
    if (!file_in.is_open())
    {
    
    
        std::cout << "open " << file_path << " failed" << std::endl;
        return false;
    }
    else
    {
    
    
        file_in >> root;
        std::cout << "load from file:\n"
                  << root << std::endl;
    }
    return true;
}

int main(){
    
    
    Json::Value root;
    writeMapInfo("./test.json", root);
    loadMapInfo("./test.json", root);
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.18)

project(Jsontest LANGUAGES C CXX)

set(JSONCPP_DIR D:/carlos/install/jsoncpp)

include_directories(${JSONCPP_DIR}/include)
link_directories(${JSONCPP_DIR}/lib)

add_executable(jsontest ./jsontest.cpp)
target_link_libraries(jsontest jsoncpp)

compile.bat

cmake -S ./src -B ./build
cmake --build ./build --config Release --target ALL_BUILD

run.bat

@REM carlos 202303
set PATH=%PATH%;D:\carlos\install\jsoncpp\bin
.\build\Release\jsontest.exe

编译运行

./compile.bat
./run.bat

输出结果:
在这里插入图片描述
linux上将上述命令对应路径修改好就行

参考

https://blog.csdn.net/jiesunliu3215/article/details/125970395
https://blog.csdn.net/King_weng/article/details/88430199

猜你喜欢

转载自blog.csdn.net/qq_41102371/article/details/129300456