jsoncpp安装与使用 cmake安装 升级g++ gcc支持c++11
来了新公司之后,现在的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; }
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!