C++中MFC实现Json格式化

头文件定义:

class CFormat
{
public:
	virtual int format(CString input, CString& output) = 0;
};

#define TAB_SPACE "    "
#define NEW_LINE  "\r\n"

函数实现代码:

int CFormatJson::format(CString input, CString& output) {
    std::string result = "";
    std::string inputData;
    int level = 0;

    inputData = CT2A(input.GetString());

    for (std::string::size_type index = 0; index < inputData.size(); index++)
    {
        char c = inputData[index];

        if (level > 0 && '\r\n' == inputData[inputData.size() - 1])
        {
            result += getLevelStr(level);
        }

        switch (c)
        {
        case '{':
        case '[':
            result = result + c + NEW_LINE;
            level++;
            result += getLevelStr(level);
            break;
        case ',':
            result = result + c;

            // 判断是否是inputData的逗号
            if ((inputData[index - 1] == '"') || (inputData[index + 1] == '"')) {
                result = result + NEW_LINE;
                result += getLevelStr(level);
            }

            break;
        case '}':
        case ']':
            result += NEW_LINE;
            level--;
            result += getLevelStr(level);
            result += c;
            break;
        default:
            // 回车键
            if (c == '\r') {
            }
            else {
                result += c;
            }
            break;
        }

    }

	// 输出
    output = result.c_str();
	return 1;
}

std::string CFormatJson::getLevelStr(int level)
{
	std::string levelStr = "";
	for (int i = 0; i < level; i++)
	{
		levelStr += TAB_SPACE;
	}
	return levelStr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 C 语言,可以使用第三方库来处理 JSON 格式的数据。常用的 JSON 库包括 cJSON、jansson 和 json-c 等。 以下是一个使用 cJSON 库来解析 JSON 格式数据的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "cJSON.h" int main() { char *json_str = "{\"name\": \"John\", \"age\": 30, \"isMarried\": true}"; cJSON *root = cJSON_Parse(json_str); if (root == NULL) { printf("Failed to parse JSON string!\n"); return -1; } cJSON *name = cJSON_GetObjectItem(root, "name"); if (name != NULL) { printf("Name: %s\n", name->valuestring); } cJSON *age = cJSON_GetObjectItem(root, "age"); if (age != NULL) { printf("Age: %d\n", age->valueint); } cJSON *is_married = cJSON_GetObjectItem(root, "isMarried"); if (is_married != NULL) { printf("Is married: %s\n", cJSON_IsTrue(is_married) ? "true" : "false"); } cJSON_Delete(root); return 0; } ``` 在上述示例代码,首先定义了一个 JSON 格式的字符串,并使用 cJSON_Parse 函数将其解析为 cJSON 结构体。然后通过 cJSON_GetObjectItem 函数获取 JSON 对象的各个属性,最后使用 cJSON_Delete 函数释放 cJSON 对象的内存。 与解析类似,使用 cJSON 库也可以将数据转换为 JSON 格式的字符串。以下是一个将数据转换为 JSON 格式字符串的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "cJSON.h" int main() { cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "name", "John"); cJSON_AddNumberToObject(root, "age", 30); cJSON_AddTrueToObject(root, "isMarried"); char *json_str = cJSON_Print(root); printf("%s\n", json_str); free(json_str); cJSON_Delete(root); return 0; } ``` 在上述示例代码,首先创建了一个 cJSON 对象,并使用 cJSON_AddXXXToObject 函数添加各个属性。然后使用 cJSON_Print 函数将 cJSON 对象转换为 JSON 格式的字符串,并在最后释放内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值