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;
}
stringWrite.cpp
会警告,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;
}
stringWrite.cpp
反序列化新旧接口

#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;
}
readFromString.cpp
jsoncpp安装与使用 cmake安装 升级g++ gcc支持c++11的更多相关文章
- 升级 GCC 支持C++11 或 configure: error: *** A compiler with support for C++11 language features is required.
configure: error: *** A compiler with support for C++11 language features is required. 参考链接: (1)升级 G ...
- g++默认支持c++11标准的办法
//第一种,直接包含在源程序文件中,如第一行代码所示 #pragma GCC diagnostic error "-std=c++11" #include <iostream ...
- 转:linux下安装或升级GCC4.8,以支持C++11标准
转:http://www.cnblogs.com/lizhenghn/p/3550996.html C++11标准在2011年8月份获得一致通过,这是自1998年后C++语言第一次大修订,对C++语言 ...
- cmake安装MySQL
发现一个网址整理的挺好,请各位参考: http://www.chenyudong.com/archives/building-mysql-5-6-from-source.html#i 也可以参考我的另 ...
- linux下安装或升级GCC4.8,以支持C++11标准
C++11标准在2011年8月份获得一致通过,这是自1998年后C++语言第一次大修订,对C++语言进行了改进和扩充.随后各编译器厂商都各自实现或部分实现了C++中的特性. 如需查看各编译器对C++1 ...
- linux 下使用 cmake安装mysql
原文地址:http://www.cppblog.com/issay789/archive/2013/01/05/196967.html 一.安装 m4 下载地址: http://files.w3pc. ...
- OpenCV+Qt+CMake安装+十种踩坑
平台:win10 x64+opencv-3.4.1 + qt-x86-5.9.0 + cmake3.13.4 x64 OpenCV+Qt+CMake安装,及目前安装完后打包:mingw32-make时 ...
- 在CentOS 6.3中安装与配置cmake
安装说明安装环境:CentOS-6.3安装方式:源码编译安装软件:cmake-2.8.10.2.tar.gz下载地址:http://www.cmake.org/cmake/resources/soft ...
- 【整理】LINUX下使用CMAKE安装MYSQL
原文地址:http://www.cppblog.com/issay789/archive/2013/01/05/196967.html 一.安装 m4 下载地址: http://files.w3pc. ...
- 转:CMake安装和使用
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX ...
随机推荐
- C#事件总结
前言:C#的事件也是一项非常关键的技术,必须要深刻的理解,本质上是基于委托的: 事件模型的五个组成部分: 1.事件的拥有者-- event source,对象: 2.事件的成员--event,成员: ...
- 机器学习策略篇:详解处理数据不匹配问题(Addressing data mismatch)
处理数据不匹配问题 如果您的训练集来自和开发测试集不同的分布,如果错误分析显示有一个数据不匹配的问题该怎么办?这个问题没有完全系统的解决方案,但可以看看一些可以尝试的事情.如果发现有严重的数据不匹配问 ...
- UE MultiLineTraceByChannel函数返回只有一个对象的问题
问题描述 MultiLineTraceByChannel,看函数名字是返回射线检测到的所有对象,实际使用过程中,发现返回的数组中只又一个对象. Multi Line Trace by Channel ...
- Midnight Commander (MC)
Midnight Commander GNU Midnight Commander 是一个可视化文件管理器,根据 GNU 通用公共许可证获得许可,因此有资格成为自由软件.它是一个功能丰富的全屏文本模式 ...
- 商业级java开发单体项目环境搭建
写在开发前,端口经常被战占,windows10用以下命令先查先杀,netstat -aon | findstr "8080" taskkill /F /PID <PID> ...
- 【教程】运行所选代码生成器时出错:“无法解析依赖项。"EntityFramework 6.4.4" 与 ' EntityFramework.zh-Hans 6.2.0 约束:EntityFramework(=6.2.0)'不兼容。"
添加包含视图的控制器 执行以上添加"包含视图的MVC5控制器(使用Entity Framework)时报错 解决方案 在解决方案资源管理器中找到packages.config 注释掉Enti ...
- Charles 4.6 小茶杯 网络抓包工具
下载官网: https://www.charlesproxy.com/download 破解网站: Charles破解工具 (zzzmode.com)
- AI/机器学习(计算机视觉/NLP)方向面试复习1
1. 判断满二叉树 所有节点的度要么为0,要么为2,且所有的叶子节点都在最后一层. #include <iostream> using namespace std; class TreeN ...
- Jmeter函数助手33-split
split函数用于根据分隔符拆分传递给它的字符串,并返回原始字符串. String to split:填入需要转换的字符串 函数名称.用于存储在测试计划中其他的方式使用的值:存储结果的变量名 Stri ...
- 【DataBase】MySQL 31 游标
游标 Cursor 游标是用来存储查询的结果集的数据类型,也称为是光标 在存储过程和函数中可以使用光标对结果集进行循环的处理 光标的使用包括1.声明,2.开启,3.关闭,4.Fetch 游标仅用于存储 ...