一、背景
二、json写操作
2.1 json文件写操作,拷贝初版
//json文件写操作,拷贝初版void json_write()
{//1. 定义对象 { }cJSON* interest = cJSON_CreateObject();// 插入元素,对应 键值对cJSON_AddItemToObject(interest, "combat", cJSON_CreateString("热血")); // 当值是字符串时,需要使用函数cJSON_CreateString()创建cJSON_AddItemToObject(interest, "reasoning", cJSON_CreateString("推理"));// 或者使用宏进行添加//cJSON_AddStringToObject(interest, "reasoning", "推理"); // 或者这样写// 2.定义 [ ] 数组cJSON* final = cJSON_CreateArray();// 往数组中添加元素cJSON_AddItemToArray(final, cJSON_CreateString("BE"));cJSON_AddItemToArray(final, cJSON_CreateString("HE"));// 3.定义 { } 对象cJSON* likeObject1 = cJSON_CreateObject();cJSON_AddItemToObject(likeObject1, "game", cJSON_CreateString("斩赤红之瞳"));cJSON_AddItemToObject(likeObject1, "Episodes", cJSON_CreateNumber(22)); // 当值是数字时,需要使用函数cJSON_CreateNumber()创建//cJSON_AddNumberToObject(likeObject1, "price", 66.6); // 或者这样写cJSON* likeObject2 = cJSON_CreateObject();cJSON_AddItemToObject(likeObject2, "game", cJSON_CreateString("文豪野犬"));cJSON_AddItemToObject(likeObject2, "Episodes", cJSON_CreateNumber(84));// 4.定义 [ ] 数组cJSON* like = cJSON_CreateArray();// 往数组中添加元素cJSON_AddItemToArray(like, likeObject1);cJSON_AddItemToArray(like, likeObject2);// 定义 [ ] 数组cJSON* education1 = cJSON_CreateArray();cJSON_AddItemToArray(education1, cJSON_CreateString("战斗"));cJSON_AddItemToArray(education1, cJSON_CreateString("热血"));cJSON* education2 = cJSON_CreateArray();cJSON_AddItemToArray(education2, cJSON_CreateString("推理"));cJSON_AddItemToArray(education2, cJSON_CreateString("格斗"));// 5.定义 [ ] 数组/*"languages": {"serialOne": { "language": "汉语", "grade" : 10 },"serialTwo" : { "language": "英语", "grade" : 6}}*/cJSON* education = cJSON_CreateArray();cJSON_AddItemToArray(education, education1);cJSON_AddItemToArray(education, education2);// 定义对象 { }cJSON* serialOne = cJSON_CreateObject();cJSON_AddItemToObject(serialOne, "language", cJSON_CreateString("汉语"));cJSON_AddItemToObject(serialOne, "grade", cJSON_CreateNumber(10));cJSON* serialTwo = cJSON_CreateObject();cJSON_AddItemToObject(serialTwo, "language", cJSON_CreateString("英语"));cJSON_AddItemToObject(serialTwo, "grade", cJSON_CreateNumber(6));// 定义对象 { }cJSON* languages = cJSON_CreateObject();cJSON_AddItemToObject(languages, "serialOne", serialOne);cJSON_AddItemToObject(languages, "serialTwo", serialTwo);// 创建跟对象cJSON* root = cJSON_CreateObject();// 将子项插入根项中cJSON_AddItemToObject(root, "name", cJSON_CreateString("Blue"));cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(25));cJSON_AddItemToObject(root, "interest", interest);cJSON_AddItemToObject(root, "final", final);cJSON_AddItemToObject(root, "like", like);cJSON_AddItemToObject(root, "education", education);cJSON_AddItemToObject(root, "languages", languages);cJSON_AddItemToObject(root, "vip", cJSON_CreateTrue()); // "vip": true 插入布尔类型数据需要使用cJSON_CreateBool函数cJSON_AddItemToObject(root, "address", cJSON_CreateNull()); // "address": null 插入NULL值需要使用cJSON_CreateNull函数//cJSON_AddTrueToObject(root, "vip");//cJSON_AddNullToObject(root, "address"); // 或者这样写也是可以的// 控制台输出char* ALL_JSON = cJSON_Print(root);char* ALL_JSONUnformatted = cJSON_PrintUnformatted(root);printf("ALL_JSON:\n%s\n", ALL_JSON); // ALL_JSON:有做格式调整printf("ALL_JSONUnformatted:\n%s\n", ALL_JSONUnformatted); // cJSON_PrintUnformatted:没有做格式调整// 返回的字符串指针需要自己释放free(ALL_JSON);free(ALL_JSONUnformatted);//****************************************************************// 打开文件FILE* file = NULL;file = fopen("E:\\abs\\test.json", "w");if (file == NULL) {printf("Open file fail!\n");// 释放指针内存cJSON_Delete(root);return;}char* cjValue = cJSON_Print(root);// 写入文件//int ret = fwrite(cjValue, sizeof(char), strlen(cjValue), file);int ret = fputs(cjValue, file);if (ret == EOF) {printf("写入文件失败!\n");}fclose(file);free(cjValue);// 释放指针内存cJSON_Delete(root);
}
2.2修改成代码规范版,包括增加指针规范安全
//json文件写操作,代码规范初级版void json_write()
{//1. 定义对象 { }cJSON* interest = cJSON_CreateObject();if(interest == nullptr){return;}// 插入元素,对应 键值对cJSON_AddItemToObject(interest, "combat", cJSON_CreateString("热血")); // 当值是字符串时,需要使用函数cJSON_CreateString()创建cJSON_AddItemToObject(interest, "reasoning", cJSON_CreateString("推理"));// 或者使用宏进行添加//cJSON_AddStringToObject(interest, "reasoning", "推理"); // 或者这样写// 2.定义 [ ] 数组cJSON* final = cJSON_CreateArray();// 往数组中添加元素cJSON_AddItemToArray(final, cJSON_CreateString("BE"));cJSON_AddItemToArray(final, cJSON_CreateString("HE"));// 3.定义 { } 对象cJSON* likeObject1 = cJSON_CreateObject();cJSON_AddItemToObject(likeObject1, "game", cJSON_CreateString("斩赤红之瞳"));cJSON_AddItemToObject(likeObject1, "Episodes", cJSON_CreateNumber(22)); // 当值是数字时,需要使用函数cJSON_CreateNumber()创建//cJSON_AddNumberToObject(likeObject1, "price", 66.6); // 或者这样写cJSON* likeObject2 = cJSON_CreateObject();cJSON_AddItemToObject(likeObject2, "game", cJSON_CreateString("文豪野犬"));cJSON_AddItemToObject(likeObject2, "Episodes", cJSON_CreateNumber(84));// 4.定义 [ ] 数组cJSON* like = cJSON_CreateArray();// 往数组中添加元素cJSON_AddItemToArray(like, likeObject1);cJSON_AddItemToArray(like, likeObject2);// 定义 [ ] 数组cJSON* education1 = cJSON_CreateArray();cJSON_AddItemToArray(education1, cJSON_CreateString("战斗"));cJSON_AddItemToArray(education1, cJSON_CreateString("热血"));cJSON* education2 = cJSON_CreateArray();cJSON_AddItemToArray(education2, cJSON_CreateString("推理"));cJSON_AddItemToArray(education2, cJSON_CreateString("格斗"));// 5.定义 [ ] 数组/*"languages": {"serialOne": { "language": "汉语", "grade" : 10 },"serialTwo" : { "language": "英语", "grade" : 6}}*/cJSON* education = cJSON_CreateArray();cJSON_AddItemToArray(education, education1);cJSON_AddItemToArray(education, education2);// 定义对象 { }cJSON* serialOne = cJSON_CreateObject();cJSON_AddItemToObject(serialOne, "language", cJSON_CreateString("汉语"));cJSON_AddItemToObject(serialOne, "grade", cJSON_CreateNumber(10));cJSON* serialTwo = cJSON_CreateObject();cJSON_AddItemToObject(serialTwo, "language", cJSON_CreateString("英语"));cJSON_AddItemToObject(serialTwo, "grade", cJSON_CreateNumber(6));// 定义对象 { }cJSON* languages = cJSON_CreateObject();cJSON_AddItemToObject(languages, "serialOne", serialOne);cJSON_AddItemToObject(languages, "serialTwo", serialTwo);// 创建跟对象cJSON* root = cJSON_CreateObject();// 将子项插入根项中cJSON_AddItemToObject(root, "name", cJSON_CreateString("Blue"));cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(25));cJSON_AddItemToObject(root, "interest", interest);cJSON_AddItemToObject(root, "final", final);cJSON_AddItemToObject(root, "like", like);cJSON_AddItemToObject(root, "education", education);cJSON_AddItemToObject(root, "languages", languages);cJSON_AddItemToObject(root, "vip", cJSON_CreateTrue()); // "vip": true 插入布尔类型数据需要使用cJSON_CreateBool函数cJSON_AddItemToObject(root, "address", cJSON_CreateNull()); // "address": null 插入NULL值需要使用cJSON_CreateNull函数//cJSON_AddTrueToObject(root, "vip");//cJSON_AddNullToObject(root, "address"); // 或者这样写也是可以的// 控制台输出char* ALL_JSON = cJSON_Print(root);char* ALL_JSONUnformatted = cJSON_PrintUnformatted(root);printf("ALL_JSON:\n%s\n", ALL_JSON); // ALL_JSON:有做格式调整printf("ALL_JSONUnformatted:\n%s\n", ALL_JSONUnformatted); // cJSON_PrintUnformatted:没有做格式调整// 返回的字符串指针需要自己释放free(ALL_JSON);free(ALL_JSONUnformatted);//****************************************************************// 打开文件FILE* file = NULL;file = fopen("E:\\abs\\test.json", "w");if (file == NULL) {printf("Open file fail!\n");// 释放指针内存cJSON_Delete(root);return;}char* cjValue = cJSON_Print(root);// 写入文件//int ret = fwrite(cjValue, sizeof(char), strlen(cjValue), file);int ret = fputs(cjValue, file);if (ret == EOF) {printf("写入文件失败!\n");}fclose(file);free(cjValue);// 释放指针内存cJSON_Delete(root);
}
2.3 json文件写操作,代码规范后应用版
# include <iostream>
# include <string>
# include "../3rd/cJSON/cJSON.h"
# include "../test_memset/include/ArrayToZero.h" using namespace std; # ifdef __cplusplus
extern "C" {
# endif void json_write ( )
{ cJSON* interest = cJSON_CreateObject ( ) ; if ( interest == nullptr ) { return ; } cJSON_AddItemToObject ( interest, "combat" , cJSON_CreateString ( "热血" ) ) ; cJSON_AddItemToObject ( interest, "reasoning" , cJSON_CreateString ( "推理" ) ) ; cJSON* final = cJSON_CreateArray ( ) ; if ( final == nullptr ) { cJSON_Delete ( interest) ; return ; } cJSON_AddItemToArray ( final , cJSON_CreateString ( "BE" ) ) ; cJSON_AddItemToArray ( final , cJSON_CreateString ( "HE" ) ) ; cJSON* likeObject1 = cJSON_CreateObject ( ) ; if ( likeObject1 == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; return ; } cJSON_AddItemToObject ( likeObject1, "game" , cJSON_CreateString ( "斩赤红之瞳" ) ) ; cJSON_AddItemToObject ( likeObject1, "Episodes" , cJSON_CreateNumber ( 22 ) ) ; cJSON* likeObject2 = cJSON_CreateObject ( ) ; if ( likeObject1 == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; return ; } cJSON_AddItemToObject ( likeObject2, "game" , cJSON_CreateString ( "文豪野犬" ) ) ; cJSON_AddItemToObject ( likeObject2, "Episodes" , cJSON_CreateNumber ( 84 ) ) ; cJSON* like = cJSON_CreateArray ( ) ; if ( like == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; return ; } cJSON_AddItemToArray ( like, likeObject1) ; cJSON_AddItemToArray ( like, likeObject2) ; cJSON* education1 = cJSON_CreateArray ( ) ; if ( education1 == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; return ; } cJSON_AddItemToArray ( education1, cJSON_CreateString ( "战斗" ) ) ; cJSON_AddItemToArray ( education1, cJSON_CreateString ( "热血" ) ) ; cJSON* education2 = cJSON_CreateArray ( ) ; if ( education2 == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; cJSON_Delete ( education1) ; return ; } cJSON_AddItemToArray ( education2, cJSON_CreateString ( "推理" ) ) ; cJSON_AddItemToArray ( education2, cJSON_CreateString ( "格斗" ) ) ; cJSON* education = cJSON_CreateArray ( ) ; cJSON_AddItemToArray ( education, education1) ; cJSON_AddItemToArray ( education, education2) ; if ( education == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; cJSON_Delete ( education1) ; cJSON_Delete ( education2) ; return ; } cJSON* serialOne = cJSON_CreateObject ( ) ; if ( serialOne == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; cJSON_Delete ( education1) ; cJSON_Delete ( education2) ; cJSON_Delete ( education) ; return ; } cJSON_AddItemToObject ( serialOne, "language" , cJSON_CreateString ( "汉语" ) ) ; cJSON_AddItemToObject ( serialOne, "grade" , cJSON_CreateNumber ( 10 ) ) ; cJSON* serialTwo = cJSON_CreateObject ( ) ; if ( serialTwo == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; cJSON_Delete ( education1) ; cJSON_Delete ( education2) ; cJSON_Delete ( education) ; cJSON_Delete ( serialOne) ; return ; } cJSON_AddItemToObject ( serialTwo, "language" , cJSON_CreateString ( "英语" ) ) ; cJSON_AddItemToObject ( serialTwo, "grade" , cJSON_CreateNumber ( 6 ) ) ; cJSON* languages = cJSON_CreateObject ( ) ; if ( languages == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; cJSON_Delete ( education1) ; cJSON_Delete ( education2) ; cJSON_Delete ( education) ; cJSON_Delete ( serialOne) ; cJSON_Delete ( serialTwo) ; return ; } cJSON_AddItemToObject ( languages, "serialOne" , serialOne) ; cJSON_AddItemToObject ( languages, "serialTwo" , serialTwo) ; cJSON* root = cJSON_CreateObject ( ) ; if ( root == nullptr ) { cJSON_Delete ( interest) ; cJSON_Delete ( final ) ; cJSON_Delete ( likeObject1) ; cJSON_Delete ( likeObject2) ; cJSON_Delete ( like) ; cJSON_Delete ( education1) ; cJSON_Delete ( education2) ; cJSON_Delete ( education) ; cJSON_Delete ( serialOne) ; cJSON_Delete ( serialTwo) ; cJSON_Delete ( languages) ; return ; } cJSON_AddItemToObject ( root, "name" , cJSON_CreateString ( "Blue" ) ) ; cJSON_AddItemToObject ( root, "age" , cJSON_CreateNumber ( 25 ) ) ; cJSON_AddItemToObject ( root, "interest" , interest) ; cJSON_AddItemToObject ( root, "final" , final ) ; cJSON_AddItemToObject ( root, "like" , like) ; cJSON_AddItemToObject ( root, "education" , education) ; cJSON_AddItemToObject ( root, "languages" , languages) ; cJSON_AddItemToObject ( root, "vip" , cJSON_CreateTrue ( ) ) ; cJSON_AddItemToObject ( root, "address" , cJSON_CreateNull ( ) ) ; char * ALL_JSON = cJSON_Print ( root) ; char * ALL_JSONUnformatted = cJSON_PrintUnformatted ( root) ; printf ( "ALL_JSON:\n%s\n" , ALL_JSON) ; printf ( "ALL_JSONUnformatted:\n%s\n" , ALL_JSONUnformatted) ; free ( ALL_JSON) ; free ( ALL_JSONUnformatted) ; FILE* file = NULL ; file = fopen ( "E:\\abs\\test.json" , "w" ) ; if ( file == NULL ) { printf ( "Open file fail!\n" ) ; cJSON_Delete ( root) ; return ; } char * cjValue = cJSON_Print ( root) ; int ret = fputs ( cjValue, file) ; if ( ret == EOF ) { printf ( "写入文件失败!\n" ) ; } fclose ( file) ; free ( cjValue) ; cJSON_Delete ( root) ;
} int main ( )
{ cout << "test CJSON" << "hello" << endl; ArrayToZero arrayToZero; arrayToZero. showArray ( ) ; json_write ( ) ; return 0 ;
} # ifdef __cplusplus
}
# endif