C++ 使用nlohmann/json.hpp库读写json字符串

1. json库

我个人比较喜欢 nlohmann/json.hpp 这个库,因为它只需要一个hpp文件即可,足够轻量!

这是它的github地址

2. 简单实例代码

#include <iostream>
#include <json.hpp>
#include <fstream>
#include <string>

using namespace nlohmann;
using namespace std;

string GenerateJsonStr()
{
    /*
    * 生成json格式字符串
    *
    * 预计的json格式文本为:
    * {
    *   "name": "ashui",
    *   "school": ["A school", "B school", "C school"],
    *   "money": [124.5, 345.3, 457.4],
    *
    *   physic
    *   {
    *       "height": 180,
    *       "weight": 68.5
    *   }
    * }
    */

    json json_data;
    json_data["name"] = "ashui";
    json_data["school"] = { "A school", "B school", "C school" };
    json_data["money"] = { 124.5, 345.3, 457.4 };

    json physic_data;
    physic_data["height"] = 180;
    physic_data["weight"] = 68.5;
    json_data["physic"] = physic_data;

    return json_data.dump();
}

bool SaveJsonStr()
{
    string jsonStr = GenerateJsonStr();

    // Save to file
    std::ofstream file("output.json");

    if (file.is_open())
    {
        file << jsonStr;

        file.close();

        std::cout << "dump string:" << std::endl;
        std::cout << jsonStr << std::endl;

        return true;
    }
    
    return false;
}

bool ReadJsonObj()
{
    std:ifstream jsonFile("D:\\Project\\C++\\json_example\\x64\\Debug\\output.json");

    if (!jsonFile.is_open())
    {
        std::cout << "Open json file error" << std::endl;
        return false;
    }

    json jsonData;
    try
    {
        jsonData = json::parse(jsonFile);

        if (jsonData.is_null())
        {
            std::cout << "The json object is null" << std::endl;
            return false;
        }

        std::string name = jsonData["name"];
        std::vector<std::string> school = jsonData["school"];
        std::vector<float> money = jsonData["money"];

        int height = jsonData["physic"]["height"];
        float weigjt = jsonData["physic"]["weight"];

        std::cout << std::endl;
        std::cout << "parse string:" << std::endl;
        std::cout << "name: " << name << std::endl;
        std::cout << "height: " << height << std::endl;
        std::cout << "weigjt: " << weigjt << std::endl;

        std::cout << "school: ";
        for (auto s : school)
        {
            std::cout << s << ", ";
        }
        std::cout << std::endl;

        std::cout << "money: ";
        for (auto m : money)
        {
            std::cout << m << ", ";;
        }
        std::cout << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cout << "json error: "<< e.what() << std::endl;
        return false;
    }

    return true;
}

int main()
{
    SaveJsonStr();
    ReadJsonObj();
}

值得注意的是,在使用[]读取json内容时,最外层一定要套一个try-catch用以补获异常,这样比起一个一个元素去判断是否为空、是否为数组等要方便且安全!

相关推荐

  1. C++ 使用nlohmann/json.hppjson字符串

    2024-05-14 13:58:08       14 阅读
  2. boostjson格式文件

    2024-05-14 13:58:08       35 阅读
  3. [json]定义、

    2024-05-14 13:58:08       35 阅读
  4. Android中C++如何json文件

    2024-05-14 13:58:08       11 阅读
  5. C# 字符串json

    2024-05-14 13:58:08       16 阅读
  6. C++

    2024-05-14 13:58:08       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-14 13:58:08       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-14 13:58:08       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-14 13:58:08       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-14 13:58:08       18 阅读

热门阅读

  1. Ubuntu下C++编程总结AllInOne

    2024-05-14 13:58:08       13 阅读
  2. Json格式备忘

    2024-05-14 13:58:08       9 阅读
  3. LinkedList源码解析

    2024-05-14 13:58:08       10 阅读
  4. git 常用命令

    2024-05-14 13:58:08       10 阅读
  5. Docker——目录迁移

    2024-05-14 13:58:08       10 阅读
  6. docker的使用

    2024-05-14 13:58:08       14 阅读
  7. 什么是机器学习?机器学习有哪些类型?

    2024-05-14 13:58:08       10 阅读
  8. autoxjs的介绍

    2024-05-14 13:58:08       11 阅读
  9. MySQL入门学习-数据修改.修改

    2024-05-14 13:58:08       8 阅读
  10. JDK介绍

    JDK介绍

    2024-05-14 13:58:08      10 阅读
  11. Oracle Exists、 NOT Exists用法

    2024-05-14 13:58:08       12 阅读