【cJSON】的相关的说明

cJSON: http://sourceforge.net/projects/cjson/
CSDN: https://blog.csdn.net/Mculover666/article/details/103796256

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

int main() {
	// 创建JSON对象并添加键值对
	cJSON *root = cJSON_CreateObject();
	cJSON_AddStringToObject(root, "StringToObject", "10");
	cJSON_AddNullToObject(root, "NullToObject");
	cJSON_AddTrueToObject(root, "TrueToObject");
	cJSON_AddFalseToObject(root, "FalseToObject");
	cJSON_AddBoolToObject(root, "BoolToObject", 1);
	cJSON_AddNumberToObject(root, "NumberToObject", 10);

	// 将JSON对象保存到文件
	FILE *file = fopen("config.json", "w");
	if (file) {
		char *jsonStr = cJSON_Print(root);
		fputs(jsonStr, file);
		fclose(file);
		free(jsonStr);
	}

	// 从文件中读取JSON对象并显示
	file = fopen("config.json", "r");
	fseek(file, 0, SEEK_END);
	long fileSize = ftell(file);
	fseek(file, 0, SEEK_SET);

	char *buffer = (char *)malloc(fileSize + 1);
	fread(buffer, 1, fileSize, file);
	buffer[fileSize] = '\0';

	cJSON *newRoot = cJSON_Parse(buffer);
	char *newJsonStr = cJSON_Print(newRoot);
	printf("JSON对象从文件中读取并显示:\n%s\n", newJsonStr);

	// 读取"StringToObject"对应的值
	cJSON *StringToObject = cJSON_GetObjectItem(newRoot, "StringToObject");
	if (StringToObject) {
		printf("EnableOutputToConsole: %s\n", StringToObject->valuestring);
	}
	else {
		printf("EnableOutputToConsole not found.\n");
	}

	// 读取"NumberToObject"对应的值
	cJSON *TrueToObject = cJSON_GetObjectItem(newRoot, "NumberToObject");
	if (TrueToObject) {
		printf("FilePath: %d\n", TrueToObject->valueint);
	}
	else {
		printf("FilePath not found.\n");
	}


	// 将JSON对象保存到变量中
	char *jsonVarStr = cJSON_Print(root);
	printf("\nJSON对象保存到变量中:\n%s\n", jsonVarStr);

	// 释放内存并清理
	free(buffer);
	cJSON_Delete(root);
	cJSON_Delete(newRoot);
	free(newJsonStr);
	free(jsonVarStr);
	fclose(file);
	system("pause");
	return 0;
}

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

void dofile(char *filename)
{
	FILE *f; long len; char *data;

	f = fopen(filename, "rb"); 
	fseek(f, 0, SEEK_END); 
	len = ftell(f); 
	fseek(f, 0, SEEK_SET);
	data = (char*)malloc(len + 1); 
	fread(data, 1, len, f);
	fclose(f);
	free(data);
}


int main() {
    // 创建最上层的JSON对象
    cJSON *root = cJSON_CreateObject();
    cJSON *menu = cJSON_CreateObject();
    cJSON *popup = cJSON_CreateObject();
    cJSON *menuitem = cJSON_CreateArray();

    // 向menu对象中添加键值对
    cJSON_AddStringToObject(menu, "id", "file");
    cJSON_AddStringToObject(menu, "value", "File");

    // 向popup对象中添加键值对
    cJSON_AddItemToObject(menu, "popup", popup);

    // 向menuitem数组中添加JSON对象
    cJSON *menuItem1 = cJSON_CreateObject();
    cJSON_AddStringToObject(menuItem1, "value", "New");
    cJSON_AddStringToObject(menuItem1, "onclick", "CreateNewDoc()");
    cJSON_AddItemToArray(menuitem, menuItem1);

    cJSON *menuItem2 = cJSON_CreateObject();
    cJSON_AddStringToObject(menuItem2, "value", "Open");
    cJSON_AddStringToObject(menuItem2, "onclick", "OpenDoc()");
    cJSON_AddItemToArray(menuitem, menuItem2);
    
    cJSON *menuItem3 = cJSON_CreateObject();
    cJSON_AddStringToObject(menuItem3, "value", "Close");
    cJSON_AddStringToObject(menuItem3, "onclick", "CloseDoc()");
    cJSON_AddItemToArray(menuitem, menuItem3);

    // 将menuitem数组添加到popup对象中
    cJSON_AddItemToObject(popup, "menuitem", menuitem);

    // 将menu对象添加到最上层的JSON对象中
    cJSON_AddItemToObject(root, "menu", menu);

    // 生成JSON字符串
    char *jsonStr = cJSON_Print(root);
    printf("%s\n", jsonStr);

	// 将JSON对象保存到文件
	FILE *file = fopen("config.json", "w");
	if (file) {
		fputs(jsonStr, file);
		fclose(file);
		free(jsonStr);
	}

    // 释放内存并清理
    //free(jsonStr);      //#include <stdlib.h>
    cJSON_Delete(root);
	system("pause");
    return 0;
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值