文章目录
一、安装jsoncpp
sudo yum -y install jsoncpp-devel
二、引入json
包含头文件<jsoncpp/json/json.h>
#include <jsoncpp/json/json.h>
三、只需要掌握三个类
jsoncpp 库中的类被定义到了一个Json命名空间中,建议在使用这个库的时候先声明这个命名空间。
using namespace Json;
1️⃣Value
类:将 json 支持的数据类型进行了包装,最终得到一个 Value 类型。
2️⃣FastWriter
类:序列化,将 Value 对象中的数据序列化为字符串。
3️⃣Reader
类:反序列化,将 json 字符串 解析成 Value 类型。
3.1 Value 类
这个类可以看做是一个包装器(中间类),它可以封装 Json 支持的所有类型,这样我们在处理数据的时候很方便。
构造函数
一般用空构造即可
Json::Value root;
检测保存数据的类型
// 检测保存的数据类型
bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isInt64() const;
bool isUInt() const;
bool isUInt64() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;
将Value对象转化为实际类型
Int asInt() const;
UInt asUInt() const;
Int64 asInt64() const;
UInt64 asUInt64() const;
LargestInt asLargestInt() const;
LargestUInt asLargestUInt() const;
JSONCPP_STRING asString() const;
float asFloat() const;
double asDouble() const;
bool asBool() const;
const char* asCString() const;
对Json数组的操作
其中append和 [ ] 都很常用
ArrayIndex size() const;
Value& operator[](ArrayIndex index);
Value& operator[](int index);
const Value& operator[](ArrayIndex index) const;
const Value& operator[](int index) const;
// 根据下标的index返回这个位置的value值
// 如果没找到这个index对应的value, 返回第二个参数defaultValue
Value get(ArrayIndex index, const Value& defaultValue) const;
Value& append(const Value& value);
const_iterator begin() const;
const_iterator end() const;
iterator begin();
iterator end();
对于Json对象的操作
Value& operator[](const char* key);
const Value& operator[](const char* key) const;
Value& operator[](const JSONCPP_STRING& key);
const Value& operator[](const JSONCPP_STRING& key) const;
Value& operator[](const StaticString& key);
// 通过key, 得到value值
Value get(const char* key, const Value& defaultValue) const;
Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
// 得到对象中所有的键值
typedef std::vector<std::string> Members;
Members getMemberNames() const;
3.2 FasterWriter/ StyledWriter 类
将结构化的数据序列化为一个长字符串,方便网络传输。
std::string Json::FastWriter::write(const Value& root);
3.3 Reader 类
第一个parse重载方法用的多
参数:
- document: json格式字符串
- root: 传出参数, 存储了json字符串中解析出的数据
- collectComments: 是否保存json字符串中的注释信息
-
bool Json::Reader::parse(const std::string& document,
Value& root, bool collectComments = true);
// 通过begindoc和enddoc指针定位一个json字符串
// 这个字符串可以是完成的json字符串, 也可以是部分json字符串
bool Json::Reader::parse(const char* beginDoc, const char* endDoc,
Value& root, bool collectComments = true);
// write的文件流 -> ofstream
// read的文件流 -> ifstream
// 假设要解析的json数据在磁盘文件中
// is流对象指向一个磁盘文件, 读操作
bool Json::Reader::parse(std::istream& is,
Value& root, bool collectComments = true);
四、序列化工作
将结构化的数据,转化为一个字符串,方便我们进行网络发送
Value是一个Json中间类,可以用它来以KV的形式填充值
// testjson.cc
#include <iostream>
#include <jsoncpp/json/json.h>
int main()
{
Json::Value root;
root["hello"] = "你好";
root["user"] = "sjj";
root["age"] = "18";
Json::StyledWriter writer;
// Json::FastWriter writer;
std::string str = writer.write(root);
std::cout << str << std::endl;
return 0;
}
编译:
g++ testjson.cc
运行结果:
[sjj@VM-20-15-centos test]$ ./a.out
{
"age" : "18",
"hello" : "你好",
"user" : "sjj"
}
###############可以换一个FastWriter
运行结果是这样的:
[sjj@VM-20-15-centos test]$ ./a.out
{"age":"18","hello":"你好","user":"sjj"}
五、反序列化工作
将一个字符串转化为多个KV值(结构化的数据)
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
// 结构化的数据
typedef struct request
{
int x;
int y;
char op; // +-*/%
} request_t;
// 注意编译时要链接json库 -ljsoncpp
int main()
{
// 反序列化的过程
std::string json_string = R"({"datax":10,"datay":20,"operator":42})";
Json::Reader reader;
Json::Value root;
reader.parse(json_string, root);
request_t req;
req.x = root["datax"].asInt();
req.y = root["datay"].asInt();
req.op = (char)root["operator"].asUInt();
std::cout << req.x << req.y << req.op << std::endl;
return 0;
}
output:
[sjj@VM-20-15-centos test]$ ./a.out
1020*