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

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

相关推荐
D_evil__5 分钟前
【Effective Modern C++】第五章 右值引用、移动语义和完美转发:24. 区分万能引用和右值引用
c++
铉铉这波能秀14 分钟前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
蜡笔小马23 分钟前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
唐梓航-求职中32 分钟前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
仟濹33 分钟前
【算法打卡day3 | 2026-02-08 周日 | 算法: BFS】3_卡码网99_计数孤岛_BFS | 4_卡码网100_最大岛屿的面积DFS
算法·深度优先·宽度优先
Ll130452529837 分钟前
Leetcode二叉树part4
算法·leetcode·职场和发展
林开落L37 分钟前
从零开始学习Protobuf(C++实战版)
开发语言·c++·学习·protobuffer·结构化数据序列化机制
林开落L40 分钟前
从入门到了解:Protobuf、JSON、XML 核心解析(C++ 示例)
xml·c++·json·protobuffer·结构化数据序列化机制
Queenie_Charlie44 分钟前
stars(树状数组)
数据结构·c++·树状数组
颜酱1 小时前
二叉树遍历思维实战
javascript·后端·算法