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

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

相关推荐
??tobenewyorker2 分钟前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
贾全23 分钟前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天38 分钟前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
oioihoii1 小时前
C++11 forward_list 从基础到精通:原理、实践与性能优化
c++·性能优化·list
满分观察网友z1 小时前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
m0_687399841 小时前
写一个Ununtu C++ 程序,调用ffmpeg API, 来判断一个数字电影的视频文件mxf 是不是Jpeg2000?
开发语言·c++·ffmpeg
森焱森2 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天2 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
Ronin3053 小时前
【C++】类型转换
开发语言·c++
闪电麦坤953 小时前
数据结构:二维数组(2D Arrays)
数据结构·算法