JSON序列化与反序列化-----使用JSON for Modern C++库

使用的三方库

JSON for Modern C++ 是一款优秀的在C++下使用的JSON库,我们使用这个三方库测试JSON的序列化和反序列化。

特点

  • 整个代码由一个头文件json.hpp组成,没有子项目,没有依赖,没有复杂的构建系统,使用起来非常方便。
  • STL和JSON容器之间可以相互转化。

项目地址

github.com/nlohmann/js...

使用

就像前面介绍的一样, 这个三方库只包含一个hpp文件,我们在项目中引入即可

下载github.com/nlohmann/js...

JSON的序列化

测试代码一

cpp 复制代码
#include <iostream>
#include <vector>
#include <map>
#include <string>

#include "json.hpp"

using namespace std;
using json = nlohmann::json;

// JSON序列化示例1
void func1() {
  json js;
  js["msg_type"] = 2;
  js["from"] = "zhangsan";
  js["to"] = "lisi";
  js["content"] = "hello lisi, I'm zhangsan";

  string str = js.dump(); // 获取字符串

  cout << str.c_str() << endl;  // string转char*输出,通过网络就可以发送了
}

int main() {

  func1();
  return 0;
}

运行输出结果

sh 复制代码
{"id":[1,2,3,4,5],"info":{"age":18,"gender":"male"},"name":"zhangsan"}

测试代码二

cpp 复制代码
#include <iostream>
#include <vector>
#include <map>
#include <string>

#include "json.hpp"

using namespace std;
using json = nlohmann::json;

void func2() {
  json js;
  // 添加数组
  js["id"] = {1, 2, 3, 4, 5};
  // 添加key-value
  js["name"] = "zhangsan";
  js["info"]["height"] = 175;
  js["info"]["weight"] = 65;
  // 添加map,一次性添加多个,相同键,后面的会覆盖前面的
  js["info"] = {{"age", 18}, {"gender", "male"}};

  string str = js.dump(); // 获取字符串

  cout << str.c_str() << endl;  // string转char*输出,通过网络就可以发送了
}

int main() {

  func2();
  return 0;
}

运行输出结果

sh 复制代码
{"id":[1,2,3,4,5],"info":{"age":18,"gender":"male"},"name":"zhangsan"}

测试代码三

cpp 复制代码
#include <iostream>
#include <vector>
#include <map>
#include <string>

#include "json.hpp"

using namespace std;
using json = nlohmann::json;


void func3() {
  json js;
  // 直接序列化一个vector容器
  vector<int> v = {1, 2, 3, 4, 5};
  js["id"] = v;

  // 直接序列化一个map容器
  map<string, int> m = {{"age", 18}, {"gender", 1}};
  js["info"] = m;

  string str = js.dump(); // 获取字符串

  cout << str.c_str() << endl;  // string转char*输出,通过网络就可以发送了
}

int main() {

  func3();
  return 0;
}

运行输出结果

sh 复制代码
{"id":[1,2,3,4,5],"info":{"age":18,"gender":1}}

JSON的反序列化

代码

cpp 复制代码
#include <iostream>
#include <vector>
#include <map>
#include <string>

#include "json.hpp"

using namespace std;
using json = nlohmann::json;

// JSON序列化示例1
string func1() {
  json js;
  js["msg_type"] = 2;
  js["from"] = "zhangsan";
  js["to"] = "lisi";
  js["content"] = "hello lisi, I'm zhangsan";

  string str = js.dump(); // 获取字符串

  return str;
}


int main() {

  string recvStr = func1();

  // 数据的反序列化
  json jsbuf = json::parse(recvStr);

  cout << jsbuf["msg_type"] << endl;
  cout << jsbuf["from"] << endl;
  cout << jsbuf["to"] << endl;
  cout << jsbuf["content"] << endl;

  return 0;
}

测试

sh 复制代码
➜  json_test ./main 
2
"zhangsan"
"lisi"
"hello lisi, I'm zhangsan"
相关推荐
草捏子4 小时前
从CPU原理看:为什么你的代码会让CPU"原地爆炸"?
后端·cpu
嘟嘟MD4 小时前
程序员副业 | 2025年3月复盘
后端·创业
胡图蛋.5 小时前
Spring Boot 支持哪些日志框架?推荐和默认的日志框架是哪个?
java·spring boot·后端
无责任此方_修行中5 小时前
关于 Node.js 原生支持 TypeScript 的总结
后端·typescript·node.js
吃海鲜的骆驼5 小时前
SpringBoot详细教程(持续更新中...)
java·spring boot·后端
迷雾骑士6 小时前
SpringBoot中WebMvcConfigurer注册多个拦截器(addInterceptors)时的顺序问题(二)
java·spring boot·后端·interceptor
uhakadotcom6 小时前
Thrift2: HBase 多语言访问的利器
后端·面试·github
Asthenia04126 小时前
Java 类加载规则深度解析:从双亲委派到 JDBC 与 Tomcat 的突破
后端
方圆想当图灵6 小时前
从 Java 到 Go:面向对象的巨人与云原生的轻骑兵
后端·代码规范
Moment6 小时前
一份没有项目展示的简历,是怎样在面试里输掉的?开源项目或许是你的救命稻草 😭😭😭
前端·后端·面试