• 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

C++ – 速通nlohmann json,nlohmann json使用教程

C++ 发布于2023-07-20 阅读 6,158次 0次评论 2次点赞 本文共4547个字,阅读需要12分钟。

1 nlohmann json开发环境配置

nlohmann json是一个C++、开源的,只有单个头文件的、高效快速的json操作库。

优点:

  • 现代C++风格,语法直观,使用简单
  • 没有复杂的编译、依赖关系,直接包含头文件即可

缺点:

  • 并不是最快的json库
  • 内存占用稍大

Github仓库:https://github.com/nlohmann/json

因为nlohmann json支持单个头文件,所以在配置其开发环境时只需要下载其发布包include.zip,解压缩该压缩包,然后将该文件下的子文件夹single_include添加到项目中头文件路径即可。

使用时,在代码中包含其头文件

#include "nlohmann/json.hpp"

即可使用,非常简单。

2 nlohmann json的使用

2.1 json解析

2.1.1 json解析中的异常和容错处理

2.1.1.1 判断字符串是否可以被解析为json对象

在将字符串解析成json对象时,需要判断该字符串是可以被解析成json对象,我们可以使用以下代码

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    const std::string json_str = "niubi";

    // 判断是否可以解析为json对象
    if (!nlohmann::json::accept(json_str))
    {
        std::cerr << "parse json failed" << std::endl;
    }

    return 0;
}

也可以使用try...catch捕捉解析异常,通过捕捉异常判断是否可以正常解析为json对象,如以下代码

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    const std::string json_str = "niubi";

    try
    {
        nlohmann::json root = nlohmann::json::parse(json_str);
    }
    catch (nlohmann::json::parse_error& e)
    {
        std::cerr << "parse json failed, error message:"<< e.what() << std::endl;
    }

    return 0;
}

如果不想让nlohmann::json::parse抛出异常,则可将

nlohmann::json root = nlohmann::json::parse(json_str);

修改为

nlohmann::json root = nlohmann::json::parse(json_str,nullptr,false);

这样就不会抛出异常了。

2.1.1.2 判断json对象是否包含键值

在解析json对象时,为了避免某个键值为空导致后续操作失败的问题,通常需要判断json是否包含某个键值,示例代码如下

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    const std::string json_str = "{\"data\":0}";

    try
    {
        nlohmann::json root = nlohmann::json::parse(json_str,nullptr,false);
        if (root.contains("data"))
        {
            int data_value = root["data"].get<int>();

            std::cout << "parse success, the value data:" << data_value << std::endl;
        }
        else
        {
            std::cerr << "json not contains data key" << std::endl;
        }
    }
    catch (nlohmann::json::parse_error& e)
    {
        std::cerr << "parse json failed, error message:"<< e.what() << std::endl;
    }

    return 0;
}
2.1.1.3 判断json对象键值对应值的数据类型

其实上一小节中的代码是有问题的,我直接使用

int data_value = root["data"].get<int>();

去获取data对应的int类型的值,如果data对应的不是int类型的话会出现错误,在不出现错误的情况下,解析出来的值也会不正确,所以应该先对data对应的值的数据类型进行判断,增强程序的健壮性,修改后的代码如下

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    const std::string json_str = "{\"data\":0}";

    try
    {
        nlohmann::json root = nlohmann::json::parse(json_str,nullptr,false);
        if (root.contains("data"))
        {
            if (root["data"].is_number_integer())
            {
                int data_value = root["data"].get<int>();

                std::cout << "parse success, the value data:" << data_value << std::endl;
            }
            else
            {
                std::cerr << "key value is not int type" << std::endl;
            }
        }
        else
        {
            std::cerr << "json not contains data key" << std::endl;
        }
    }
    catch (nlohmann::json::parse_error& e)
    {
        std::cerr << "parse json failed, error message:"<< e.what() << std::endl;
    }

    return 0;
}

2.1.2 从文本文件中解析json

nlohmann json支持直接从文件对象中解析json,示例代码如下

#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"

int main()
{
    const std::string file_path = "example.json";

    std::ifstream file(file_path);

    try
    {
        nlohmann::json root = nlohmann::json::parse(file);
    }
    catch (nlohmann::json::parse_error& e)
    {
        std::cerr << "parse json failed, error message:" << e.what() << std::endl;
    }

    return 0;
}

2.1.3 从字符串中解析json

nlohmann json支持直接从字符串中解析json,示例代码如下

#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"

int main()
{
    const std::string json_str = "{\"data\":1,\"func_code\":10001}";

    try
    {
        nlohmann::json root = nlohmann::json::parse(json_str);
    }
    catch (nlohmann::json::parse_error& e)
    {
        std::cerr << "parse json failed, error message:" << e.what() << std::endl;
    }

    return 0;
}

2.2 创建json对象

通过nlohmann json我们可以很简单就可以创建json对象,比如

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::json root;
    root["pi"] = 3.141;
    root["happy"] = true;
    root["name"] = "Niels";
    root["nothing"] = nullptr;
    root["answer"]["everything"] = 42;
    root["list"] = { 1, 0, 2 };
    root["object"] = { {"currency", "USD"}, {"value", 42.99} };

    return 0;
}

也可以这样

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::json root = {
      {"pi", 3.141},
      {"happy", true},
      {"name", "Niels"},
      {"nothing", nullptr},
      {"answer", {
        {"everything", 42}
      }},
      {"list", {1, 0, 2}},
      {"object", {
        {"currency", "USD"},
        {"value", 42.99}
      }}
    };

    return 0;
}

也可以从输入流中创建json

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::json root;
    std::cin >> root;

    return 0;
}

2.3 json对象序列化为字符串

nlohmann json支持将json对象序列化为字符串,示例代码如下

#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::json root;
    root["pi"] = 3.141;
    root["happy"] = true;
    root["name"] = "Niels";
    root["nothing"] = nullptr;
    root["answer"]["everything"] = 42;
    root["list"] = { 1, 0, 2 };
    root["object"] = { {"currency", "USD"}, {"value", 42.99} };

    std::string json_str = root.dump();

    std::cout << json_str << std::endl;

    return 0;
}

2.4 json对象保存为json文件

#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::json root;
    root["pi"] = 3.141;
    root["happy"] = true;
    root["name"] = "Niels";
    root["nothing"] = nullptr;
    root["answer"]["everything"] = 42;
    root["list"] = { 1, 0, 2 };
    root["object"] = { {"currency", "USD"}, {"value", 42.99} };

    std::string json_str = root.dump();

    // 写入json文件
    std::ofstream ofs("example.json");
    ofs << json_str;
    ofs.close();

    return 0;
}

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++ – 速通nlohmann json,nlohmann json使用教程

原文链接:https://www.stubbornhuang.com/2731/

发布于:2023年07月20日 11:43:03

修改于:2023年09月27日 11:17:21

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
C++ - 基于no-boost Asio实现一个异步TCP服务器
C++
下一篇
C++ - String literal,字符串关键字R,L,u8,u,U的作用
C++
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号