jsoncpp的编译和使用推荐文章:jsoncpp的编译和使用 | 爱编程的大丙 (subingwen.cn)https://www.subingwen.cn/cpp/jsoncpp/从这个链接下载jsoncpp-master:https://github.com/open-source-parsers/jsoncpp
可以把这个文件夹名字改成jsoncpp,改短点 (●'◡'●),路径为D:/jsoncpp
下载 cmake 工具:我下载的是3.28.1版本的
先创建一个文件夹output-project,路径为D:\Work\output-project;
接着运行cmake-gui.exe
选好source code路径和build the binaries路径,点击configure,由于我的电脑安装的是visual stdio 2022, 所以我选择vs2022的,最后点击Generate
1.制作lib文件夹
(1)查看生成的文件,在output-project文件夹中lib中的Debug中有jsoncpp.lib文件
(2)在output-project文件夹中bin中的Debug中有jsoncpp.dll文件
(3)我们可以在桌面先创建一个文件夹叫lib,用来存放拷贝出来的jsoncpp.lib文件和jsoncpp.dll文件
2. 制作inlude文件夹
(1)在桌面先创建一个文件夹叫include文件夹,我们在D:/jsoncpp/include/son中的这些文件拷贝到include中
3.制作src文件夹
(1)在桌面先创建一个文件夹叫src文件夹,我们在D:/jsoncpp/src/lib_json中的这些文件拷贝到src中
为了后续更好的管理jsoncpp的文件和其他库,我们可以在src中创建一个json文件夹,将*.cpp的文件都剪切进去。至此,我们就完成了这个项目的基本结构
PS D:\Work\mysqlConnPool> tree
卷 Data 的文件夹 PATH 列表
卷序列号为 02DE-5B5B
D:.
├─.vscode
├─include
│ └─json
├─lib
└─src└─json
PS D:\Work\mysqlConnPool>
test.cpp
#include <iostream>
#include "json.h"
#include <fstream>
using namespace std;
using namespace Json;void writeJson() {Value root;root.append("luffy");root.append(19);root.append(170);root.append(false);Value subArray;subArray.append("ace");subArray.append("sabo");root.append(subArray);Value subObj;subObj["sex"] = "man";subObj["girlfriend"] = "Hancock";root.append(subObj);
#if 1string json = root.toStyledString();
#elseFastWriter w;string json = w.write(root);
#endif// write -> ofstream// read -> ifstreamofstream ofs("./test.json");ofs << json;ofs.close();return ;
}void readJson() {ifstream ifs("test.json");Reader rd;Value root;rd.parse(ifs,root);if(root.isArray()){for(unsigned i = 0; i < root.size(); ++i){Value item = root[i];if(item.isInt()) {cout<<item.asInt()<<", ";}else if(item.isString()){cout<<item.asString()<<", ";}else if(item.isBool()){cout<<item.asBool()<<", ";}else if(item.isArray()){for(unsigned j=0; j<item.size(); ++j){cout<<item[j].asString()<<", ";}}else if(item.isObject()){Value::Members keys = item.getMemberNames();for(int k=0; k<keys.size(); ++k) {cout<<keys.at(k)<<":"<<item[keys[k]]<<",";}}cout<<endl;}}
}
int main() {writeJson();readJson();cout<<"heheda"<<endl;Value root;return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.28.1)
project(test)# 添加头文件路径
include_directories(${PROJECT_SOURCE_DIR}/include/json)
# 源文件
aux_source_directory(${PROJECT_SOURCE_DIR}/src/json SRC_LIST)# 添加库文件路径
link_directories(${PROJECT_SOURCE_DIR}/lib)
# 添加可执行文件
add_executable(app test.cpp ${SRC_LIST})
target_link_libraries(app jsoncpp)# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量
然后我们把项目中的src文件夹中的json文件夹中的cpp文件的包含头文件改一下,改成如下格式:
PS D:\Work\mysqlConnPool> mkdir build目录: D:\Work\mysqlConnPoolMode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/1/19 17:22 buildPS D:\Work\mysqlConnPool> cd build
PS D:\Work\mysqlConnPool\build> cmake ..
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: D:/Work/mysqlConnPool/build
PS D:\Work\mysqlConnPool\build> make
[ 20%] Building CXX object CMakeFiles/app.dir/test.cpp.obj
[ 40%] Building CXX object CMakeFiles/app.dir/src/json/json_reader.cpp.obj
[ 60%] Building CXX object CMakeFiles/app.dir/src/json/json_value.cpp.obj
[ 80%] Building CXX object CMakeFiles/app.dir/src/json/json_writer.cpp.obj
[100%] Linking CXX executable D:\Work\mysqlConnPool\bin\app.exe
[100%] Built target app
PS D:\Work\mysqlConnPool\build> cd ../bin
PS D:\Work\mysqlConnPool\bin> .\app.exe
luffy,
19,
170,
0,
ace, sabo,
girlfriend:"Hancock",sex:"man",
heheda
PS D:\Work\mysqlConnPool\bin>
我们可以在该项目的bin目录下看到生成一个test.json文件
["luffy",19,170,false,["ace","sabo"],{"girlfriend" : "Hancock","sex" : "man"}
]