赞
踩
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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。