网站建设初学软件,摄影作品网站或app,哈市那里网站做的好,建筑工地招工网文章目录 1. 下载 json2. 基本使用value 类型#xff1a;万能类型Writer 类型#xff1a;序列化Reader 类型#xff1a;反序列化 3. 使用举例 1. 下载 json 
yum makecache fast
yum -y install jsoncpp-devel2. 基本使用 
头文件#xff1a; 
#include jsoncpp/json/j… 文章目录 1. 下载 json2. 基本使用value 类型万能类型Writer 类型序列化Reader 类型反序列化  3. 使用举例 1. 下载 json 
yum makecache fast
yum -y install jsoncpp-devel2. 基本使用 
头文件 
#include jsoncpp/json/json.h编译时需要加上 -ljsoncpp 
value 类型万能类型 json::value是一种万能对象类型能接收任意 kv 类型储存的值会自动转成 字符串类型  使用 中括号 [] 进行 定义 和 拿取  asInt()可以将提取的 string转化成 int 类型  
Writer 类型序列化 json::FastWriter用于快速序列化的数据类型即可以将 struct 转成 string用于传输  json::StyleWriter也是用于序列化的数据类型格式更加清楚美观  write()将 value 类以序列化 string 格式写入网络  
Reader 类型反序列化 json::Reader用于反序列化的数据类型即可以将从网络接收到的 stirng 转成 struct用于业务处理  json::StyleWriter也是用于序列化格式更加清楚美观  parse()从相应的流网络里面将 string 信息读取到 json::Value 中去  使用中括号可以对内容进行提取asInt() 可以将 string 转化成 int 类型  
3. 使用举例 
此处举例协议内容为整数的加减乘除取模的运算及输出方式。 
// 【请求】
class Request
{
public:Request() {}Request(int x, int y, char op) : _x(x), _y(y), _op(op){}// struct-string// _x _op _ybool Serialize(std::string *outStr){*outStr  ;Json::Value root; root[x]  _x; root[y]  _y;root[op]  _op;Json::StyledWriter writer;	// Json::FastWriter writer; *outStr  writer.write(root);return true;}// string-structbool Deserialize(const std::string inStr){Json::Value root;Json::Reader reader; reader.parse(inStr, root);_x  root[x].asInt();     // json::Value root 里面全是string需要类型转换一下_y  root[y].asInt();_op  root[op].asInt();   // op也转成int放入char里会被解释成操作符return true;}~Request() {}public:int _x;int _y;char _op;
};// 【相应】
class Response
{
public:Response() {}Response(int result, int code) : _result(result), _code(code){}// struct-stringbool Serialize(std::string *outStr){*outStr  ;Json::Value root;root[result]  _result;root[code]  _code;// Json::FastWriter writer;Json::StyledWriter writer;*outStr  writer.write(root);return true;}// string-structbool Deserialize(const std::string inStr){Json::Value root;Json::Reader reader;reader.parse(inStr, root);_result  root[result].asInt();_code  root[code].asInt();return true;}~Response() {}public:int _result;	// 计算结果int _code; 		// 正确和错误码
};如果本文对你有些帮助请给个赞或收藏你的支持是对作者大大莫大的鼓励(✿◡‿◡) 欢迎评论留言~~