文章目录
- 🍎jsoncpp简述
- 🍎jsoncpp安装及使用
- 🍇安装
- 🍇测试使用
- 🍎jsoncpp库的使用demo

学习用到jsoncpp这个库,首先要知道什么是json,它以怎样的形式存在。
JSON是一种轻量级的数据交换格式,它的语法规则如下
- 由 “键”:值
- 其中的值可以是双引号包起来的字符串、数、布尔类型、null、对象或者数组
- 上面所说的大家都很熟悉,只有最后两个是比较特殊的
- 对象:一个对象由花括号{}包起来,每一个键:值之间用逗号, 隔开,如 {“name”: Any, “age”:18}
- 数组:一个数组由中括号[]包起来,每一个键:值之间用逗号, 隔开,如 [“friend1”: William, “friend2”: Austy]
🍎jsoncpp简述
jsoncpp中含有三种基础类:Value、Write、Reader
jsoncpp所有的对象、类型都在namespace json中,使用这个库只需要加入头文件"json.h"即可
- Json::Value
这是jsoncpp中用于存储数据的类,用于表示各种类型的对象或数组,源码中可查找到它支持的类型
enum ValueType
{
nullValue = 0, // null类型
intValue, // 有符号整型
uintValue, // 无符号整型
realValue, // double类型
stringValue, // 字符串类型
boolValue, // 布尔类型
arrayValue, // 数组类型
objectValue // 对象类型
}这里有两个点需要知道:1. jsoncpp数组可以是任意类型的值、2. value插入值后,输出的值按字母表顺序排列
- Json::Writer
这个类负责将内存中的value对象转换为json文档,输出到文件或者字符串中
它有两种主要的方法:FastWriter、StyledWriter
FastWriter:快速无格式的将value转换成json文档
StyledWriter:有格式的将value转换成json文档 - Json::Reader
用于读取json文档,或者说是用于将字符串或者文件输入流转换为Json::Value对象 - 实用函数
- 判断某个键是否存在
bool Json::Value::isMember(const char *key) const;若存在则返回1,反之为0 - 得到Value中的所有键
Json::Value::getMemberBames() const;返回一个string类型的vector - 删除某个键
Json::Value::removeMember(const char *key);返回删除的值或者null
🍎jsoncpp安装及使用
🍇安装
我这里的环境是Ubuntu 16.04,不是Windows的环境
- GitHub上clonejsoncpp的源码
https:///open-source-parsers/jsoncpp - 进入到jsoncpp目录,创建一个新目录build
mkdir -p build - 进入新建立的目录,进行编译安装(这里最好一开始就切换root用户)

cd build
cmake ../
sudo make
sudo make install


🍇测试使用
#include <iostream>
#include <jsoncpp/json/json.h>
using namespace std;
int main()
{
string json1 = "{\"name":\"网站\",\"num\":3,\"sites\":[ \"Google\", \"Shouce\", \"Taobao\"]}";
Json::Reader read;
Json::Value value;
read.parse(json1, value);
cout << "name:" << JSONCPP_STRING(value.get("name", 0).toStyledString());
cout << "num:" << value.get("num", 0).toStyledString();
for(int i=0; i<3; i++)
{
cout << "网站:" << value.get("sites", 0)[i].toStyledString();
}
return 0;
}
// 编译时需要使用到jsoncpp的链接库,否则会编译失败,错误为Json的所有类都未定义
g++ main.cpp -ljsoncpp
这里碰到错误,说是jsoncpp中的函数名已替换,应该是版本不同,函数名不一样
解决办法为:强制使用现版本的API
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif
🍎jsoncpp库的使用demo
通过上面,大概知道了jsoncpp库的相关基础知识,已经可以自主的进行简单的demo编写了
下面就是仿照例子,使用Writer写入一个json文档,再通过Reader读取这个json文档的例子
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif
using namespace std;
int createjson()
{
Json::Value root;
Json::Value language;
Json::Value mail;
Json::StyledWriter writer;
// Json::FastWriter writer;
root["Name"] = "pikashu";
root["Age"] = 18;
language[0] = "C++";
language[1] = "Python";
root["Language"] = language;
mail["QQ"] = "789123456@";
mail["Google"] = "789123456@";
root["E-mail"] = mail;
string json_file = writer.write(root);
ofstream ofs;
ofs.open("test1.json");
if(!ofs.is_open())
{
cout << "open file error." << endl;
return -1;
}
ofs<<json_file;
ofs.close()
return 0;
}
int readjson()
{
Json::Reader reader;
Json::Value root;
Json::Value language;
Json::Value mail;
ifstream ifs;
ifs.open("test1.json");
if(!ifs.is_open())
{
cout << "open file error." << endl;
return -1;
}
if (!reader.parse(ifs,root))
{
cout <<"parse error" << endl;
return -1;
}
string Name = root["Name"].asString();
int Age = root["Age"].asInt();
cout << "Name: " << Name << endl;
cout << "Age: " << Age << endl;
if(root["language"].isArray())
{
Json::Value array_l = root["language"];
cout << "Language: ";
for(int i=0; i<array_l.size(); i++)
{
cout << array_l[i] << " ";
}
cout << endl;
}
cout << "Google: " << root["E-mail"].get("Google", "").asString() << endl;
cout << "QQ: " << root["E-mail"].get("QQ", "").asString() << endl;
}
int main()
{
createjson();
readjson();
return 0;
}


















