C++引入json库
环境Mac+Clion
首先,准备cppjson文件。
GitHub上拔下来。
github地址:https://github.com/open-source- parsers/jsoncpp#generating-amalgamated-source-and-header
step1:
将jsoncpp下的include和lib文件夹放入c++项目根目录下,如下:
step2
接下来需要找到三个文件夹:
1. include
2. lib
3. include/json
复制其路径放入c++项目下的CMakeLists.txt文件里。如下
include_directories("/usr/local/jsoncpp/1.9.5/include")
include_directories("/usr/local/jsoncpp/1.9.5/include/json")
include_directories("/usr/local/jsoncpp/1.9.5/lib")
set(CMAKE_CXX_STANDARD 14)
add_executable(json_test main.cpp)
target_link_libraries(json_test /usr/local/jsoncpp/1.9.5/lib/libjsoncpp.25.dylib)
step3
使用一下代码测试:
#include <iostream>
#include "json/json.h"
using namespace std;
int main()
{
Json::Value write_value;
write_value["id"] = 123;
write_value["name"] = "James";
string str_json = write_value.toStyledString();
Json::Reader reader;
Json::Value read_value;
if (reader.parse(str_json, read_value))
{
if (!read_value["id"].isNull())
{
cout << read_value["id"].asInt() << endl;
cout << read_value["name"].asString() << endl;
}
}
return 0;
}