当前位置:   article > 正文

c++中超级好用的json库--json for morden c++_c++ json库

c++ json库

json简介

json是一种轻量简单的数据结构,常用于网络数据传输,相较于xml他更加简介轻量,相较于protobuf它的学习成本更低几乎看个十几分钟就能上手,因为就是一种键值对的形式类似于集合

{“acceptID”:“456789156”,“map”:[[1,“123”],[2,“456”],[3,“789”]],“msg”:“hello world”,“sendID”:“32154646”,“type”:1,“vec”:[1,2,3,4]}
格式化之后变为下面这样:

{
    "acceptID":"456789156",
    "map":[
        [
            1,
            "123"
        ],
        [
            2,
            "456"
        ],
        [
            3,
            "789"
        ]
    ],
    "msg":"hello world",
    "sendID":"32154646",
    "type":1,
    "vec":[
        1,
        2,
        3,
        4
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在线解析json的工具

https://www.json.cn/blog/.git/json/json/
在这里插入图片描述

json for morden c++简介

c++中还是有很多的json库的比如libjson,jsoncpp…
但是用起来比较麻烦需要链接库啊什么的,而且我们使用json一般只需要用到序列化和反序列化这两个功能所以只需要轻量级的json库就能满足我们的需求了,那么json for morden c++就是你最好的选择

json for morden c++是一个德国的大佬写的在github上已经有将近40k的star数了,
github地址:https://github.com/nlohmann/json

优点:
1、引入工程非常简单:只需要把github上的single_include目录下的json.hpp拷到你的项目中就行了
在这里插入图片描述

2、使用简单:核心类就是nlohmann::json
重载了[]运算符,添加键值对,json[key]=value;就行了
序列化方法:dump
反序列化:json::parse(json字符串)

3、和STL中的容器高度兼容
可以直接把容器当作键值对的值

4、跨平台
基于c++11写的所以在wlin或者linux下使用都只需要把json.hpp拷进来就可以了

5、模板编程的优秀范例
全部使用模板,想学习模板编程可以好好研究一下它的源码

缺点:
只能识别utf-8的字符

简单使用

#include "json.hpp"
#include <iostream>
#include <vector>
#include <map>
using json = nlohmann::json;

// 序列化测试
// json对象序列化成json字符串
std::string Serialize()
{
    json js;
    std::vector<int> vec = {1, 2, 3, 4};
    std::map<int, std::string> map_;
    map_.insert(std::make_pair<int, std::string>(1, "哈哈哈"));
    map_.insert(std::make_pair<int, std::string>(2, "啦啦啦"));
    map_.insert(std::make_pair<int, std::string>(3, "呀呀呀"));
    js["type"] = 1;
    js["send"] = "张三";
    js["accept"] = "李四";
    js["msg"] = "李四,你在干嘛";
    js["vec"] = vec;
    js["map"] = map_;
    std::cout << js << std::endl;
    return js.dump();
}
// 反序列化
void Deserialize()
{
    json js = json::parse(Serialize());
    std::cout << "type:" << js["type"] << std::endl;
    std::cout << "send:" << js["send"] << std::endl;
    std::cout << "accept:" << js["accept"] << std::endl;
    std::cout << "msg:" << js["msg"] << std::endl;
    std::cout << "vec:" << js["vec"] << std::endl;
    std::map<int, std::string> j_map = js["map"];
    std::cout << "map:" << std::endl;
    for (auto &pr : j_map)
        std::cout << pr.first << "," << pr.second << std::endl;
}

int main()
{
    Serialize();
    /*
    {"accept":"李四","map":[[1,"哈哈哈"],[2,"啦啦啦"],[3,"呀呀呀"]],"msg":"李四,你在干嘛","send":"张三","type":1,"vec":[1,2,3,4]}
    */
    Deserialize();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

效果:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/595693
推荐阅读
相关标签
  

闽ICP备14008679号