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;
}
输出

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

相关推荐
xlq2232213 分钟前
19.模版进阶(上)
c++
yuuki23323315 分钟前
【C++】初识C++基础
c语言·c++·后端
小年糕是糕手15 分钟前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
玫瑰花店34 分钟前
SomeIP报文详解
c++·someip
kupeThinkPoem41 分钟前
跳表有哪些算法?
数据结构·算法
利刃大大1 小时前
【c++中间件】redis介绍 && redis-plus-plus库使用
c++·redis·中间件
前端小L1 小时前
图论专题(二十一):并查集的“工程应用”——拔线重连,修复「连通网络」
数据结构·算法·深度优先·图论·宽度优先
永不停转1 小时前
关于 QGraphicsItemGroup 内部项目发生变化后group重新定位的问题
c++·qt
88号技师1 小时前
2025年9月一区SCI-孤行尺蠖觅食优化算法Solitary Inchworm Foraging-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
前端小L1 小时前
图论专题(二十五):最小生成树(MST)——用最少的钱,连通整个世界「连接所有点的最小费用」
算法·矩阵·深度优先·图论·宽度优先