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

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

相关推荐
wanderist.1 小时前
C++输入输出的一些问题
开发语言·c++·图论
金色熊族1 小时前
MV结构下设置Qt表格的代理(2)
c++·qt
寻星探路1 小时前
【算法专题】哈希表:从“两数之和”到“最长连续序列”的深度解析
java·数据结构·人工智能·python·算法·ai·散列表
!停1 小时前
C语言单链表
c语言·数据结构·算法
Morwit2 小时前
Qt qml创建c++类的单例对象
开发语言·c++·qt
June`2 小时前
IO模型全解析:从阻塞到异步(高并发的reactor模型)
linux·服务器·网络·c++
YxVoyager2 小时前
Qt C++ :QRegularExpression 正则表达式使用详解
c++·qt·正则表达式
闻缺陷则喜何志丹2 小时前
【回文 字符串】3677 统计二进制回文数字的数目|2223
c++·算法·字符串·力扣·回文
李余博睿(新疆)2 小时前
c++分治算法
c++
Tisfy2 小时前
LeetCode 0085.最大矩形:单调栈
算法·leetcode·题解·单调栈