來了新公司之后,現在的json解析真的很難用,舉個例子,假如想刪除一個對象,要重新生成,去掉要刪除的,其余的要組裝上。很懷念之前用的jsoncpp,想引進來,就研究一下。
下載和安裝
下載
從github,直接搜jsoncpp就能搜到,第一個就是,懶得搜直接給你地址:https://github.com/open-source-parsers/jsoncpp
安裝
python amalgamate.py
然后執行
cmake CMakeLists.txt
沒有安裝cmake,可以參考這篇博客:https://www.cnblogs.com/liudw-0215/p/9877290.html
新版cmake對gcc版本,用的centos6.5,是需要升級的,可以參考這篇博客:https://blog.csdn.net/centnetHY/article/details/81284657,但升級gcc,比較耗時。
然后再執行
make
如果想把頭文件和庫安裝到系統目錄,就執行
make install
使用
序列化新舊接口
代碼如下:

#include "json/json.h" #include <iostream> /** \brief Write a Value object to a string. * Example Usage: * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite * $./stringWrite * { * "action" : "run", * "data" : * { * "number" : 1 * } * } */ int main() { 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; }
會警告,is deprecated: Use StreamWriterBuilder instead [-Wdeprecated-declarations],因為這舊的接口,如果不想報警告,可以在代碼最上面加上下面代碼:
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif
代碼如下:

#if defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #elif defined(_MSC_VER) #pragma warning(disable : 4996) #endif #include "json/json.h" #include <iostream> /** \brief Write a Value object to a string. * * Example Usage: * * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite * * $./stringWrite * * { * * "action" : "run", * * "data" : * * { * * "number" : 1 * * } * * } * */ int main() { 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; }
反序列化新舊接口

#include "json/json.h" #include <iostream> /** * \brief Parse a raw string into Value object using the CharReaderBuilder * class, or the legacy Reader class. * Example Usage: * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString * $./readFromString * colin * 20 */ int main() { 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; }