C++ 自定义Json导出

format.h
cpp 复制代码
#ifndef FORMAT_H
#define FORMAT_H

#include <string>

namespace FormatJson {
static const char *kJsonArrayEmpty  = "[]";
static const char *kJsonObjectEmpty = "{}";
}  // namespace FormatJson

class Format
{
public:
    static void        outputJsonValue(std::string &json, const std::string &key, const std::string &value, bool isEnd);
    static void        outputJsonObject(std::string &jsonObj, const std::string &key, const std::string &value, bool isEnd);
    static void        outputJsonValueToArray(std::string &jsonArray, const std::string &jsonValue, bool isEnd);
    static void        outputJsonObjectToArray(std::string &jsonArray, const std::string &jsonValue, bool isEnd);
};

#endif  // FORMAT_H
format.cpp
cpp 复制代码
#include "format.h"

void Format::outputJsonValue(std::string &jsonObj, const std::string &key, const std::string &value, bool isEnd)
{
    if (jsonObj.empty())
        jsonObj.append("{");
    else
        jsonObj.append(", ");
    jsonObj.append("\"");
    jsonObj.append(key);
    jsonObj.append("\"");
    jsonObj.append(":");
    jsonObj.append("\"");
    jsonObj.append(value);
    jsonObj.append("\"");
    if (isEnd)
        jsonObj.append("}");
}

void Format::outputJsonObject(std::string &jsonObj, const std::string &key, const std::string &value, bool isEnd)
{
    if (jsonObj.empty())
        jsonObj.append("{");
    else
        jsonObj.append(", ");
    jsonObj.append("\"");
    jsonObj.append(key);
    jsonObj.append("\"");
    jsonObj.append(":");
    jsonObj.append(value);
    if (isEnd)
        jsonObj.append("}");
}

void Format::outputJsonValueToArray(std::string &jsonArray, const std::string &jsonValue, bool isEnd)
{
    if (jsonArray.empty())
        jsonArray.append("[");
    else
        jsonArray.append(", ");
    jsonArray.append("\"");
    jsonArray.append(jsonValue);
    jsonArray.append("\"");
    if (isEnd)
        jsonArray.append("]");
}

void Format::outputJsonObjectToArray(std::string &jsonArray, const std::string &jsonValue, bool isEnd)
{
    if (jsonArray.empty())
        jsonArray.append("[");
    else
        jsonArray.append(", ");
    jsonArray.append(jsonValue);
    if (isEnd)
        jsonArray.append("]");
}
test.cpp
cpp 复制代码
#include "format.h"

void test()
{
    // 根对象
    std::string root;
    Format::outputJsonValue(root, "numbers", "123", false);

    // 对象 + 数组
    std::vector<std::string> fruits{"apple", "banana", "orange", "mango"};
    std::string              jsonArray;
    for (const auto &fruit : fruits)
        Format::outputJsonValueToArray(jsonArray, fruit, false);
    if (!jsonArray.empty())
        jsonArray.append("]");
    if (jsonArray.empty())
        jsonArray = FormatJson::kJsonArrayEmpty;
    Format::outputJsonObject(root, "fruits", jsonArray, false);

    // 数组 + 对象
    std::string jsonObjectArray;
    std::string jsonObject;
    Format::outputJsonValue(jsonObject, "key", "value", true);
    Format::outputJsonObjectToArray(jsonObjectArray, jsonObject, false);
    Format::outputJsonObjectToArray(jsonObjectArray, jsonObject, true);

    Format::outputJsonObject(root, "objects", jsonObjectArray, false);
    Format::outputJsonValue(root, "branchs", "123", true);

    std::cout << root << std::endl;
}
输出

创作不易,小小的支持一下吧!

相关推荐
SunkingYang8 分钟前
详细介绍C++中捕获异常类型的方式有哪些,分别用于哪些情形,哪些异常捕获可用于通过OLE操作excel异常
c++·excel·mfc·异常捕获·comerror
Han.miracle2 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
北冥湖畔的燕雀3 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
mit6.8244 小时前
前后缀分解
算法
你好,我叫C小白5 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Larry_Yanan7 小时前
QML学习笔记(四十二)QML的MessageDialog
c++·笔记·qt·学习·ui
寂静山林7 小时前
UVa 10228 A Star not a Tree?
算法
Neverfadeaway7 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
代码搬运媛7 小时前
【架构相关】tsconfig.json 与 tsconfig.node.json、tsconfig.app.json 的关系和作用
node.js·json
R-G-B7 小时前
【35】MFC入门到精通——MFC运行 不显示对话框 MFC界面不显示
c++·mfc·mfc运行 不显界面·mfc界面不显示