1 Star 0 Fork 0

夜夜亮晶晶/从零实现JsonRpc框架

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
unSerialize.cpp 992 Bytes
一键复制 编辑 原始数据 按行查看 历史
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <jsoncpp/json/json.h>
//数据反序列化
bool unSerialize(std::string &body, Json::Value &val)
{
//定义工厂类
Json::CharReaderBuilder crb;
std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
//错误信息缓冲区
std::string errs;
bool ret = cr->parse(body.c_str(), body.c_str() + body.size(), &val, &errs);
if (ret == false)
{
std::cout << "unSerialize Error!";
return false;
}
return true;
}
int main()
{
//序列化数据
std::string s = R"({"姓名":"小红", "性别":"女", "年龄":18})";
//反序列化后的结果填入 Json::Value 中
Json::Value stu;
bool ret = unSerialize(s, stu);
if (ret == false)
{
return -1;
}
std::cout << "姓名:" << stu["姓名"].asString() << " 性别:" << stu["性别"].asString() << " 年龄:" << stu["年龄"].asString() << std::endl;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bright-and-sparkling-at-night/JsonRpc.git
git@gitee.com:bright-and-sparkling-at-night/JsonRpc.git
bright-and-sparkling-at-night
JsonRpc
从零实现JsonRpc框架
master

搜索帮助