赞
踩
接上文json文件写操作
#include <iostream> #include <string> #include "../3rd/cJSON/cJSON.h" #include "../test_memset/include/ArrayToZero.h" using namespace std; void AddLikeObject(cJSON* interest, cJSON* final, cJSON* likeObject1, cJSON* likeObject2, cJSON* like) { // 插入元素,对应 键值对 cJSON_AddItemToObject(interest, "combat", cJSON_CreateString("热血")); // 当值是字符串时,需要使用函数cJSON_CreateString()创建 cJSON_AddItemToObject(interest, "reasoning", cJSON_CreateString("推理")); // 或者使用宏进行添加 //cJSON_AddStringToObject(interest, "reasoning", "推理"); // 或者这样写 // 2.定义 [ ] 数组 // 往数组中添加元素 cJSON_AddItemToArray(final, cJSON_CreateString("BE")); cJSON_AddItemToArray(final, cJSON_CreateString("HE")); // 3.定义 { } 对象 cJSON_AddItemToObject(likeObject1, "game", cJSON_CreateString("斩赤红之瞳")); cJSON_AddItemToObject(likeObject1, "Episodes", cJSON_CreateNumber(22)); // 当值是数字时,需要使用函数cJSON_CreateNumber()创建 //cJSON_AddNumberToObject(likeObject1, "price", 66.6); // 或者这样写 cJSON_AddItemToObject(likeObject2, "game", cJSON_CreateString("文豪野犬")); cJSON_AddItemToObject(likeObject2, "Episodes", cJSON_CreateNumber(84)); // 4.定义 [ ] 数组 // 往数组中添加元素 cJSON_AddItemToArray(like, likeObject1); cJSON_AddItemToArray(like, likeObject2); } void AddEducationObject(cJSON* education1, cJSON* education2, cJSON* education, cJSON* serialOne, cJSON* serialTwo, cJSON* languages) { // 定义 [ ] 数组 cJSON_AddItemToArray(education1, cJSON_CreateString("战斗")); cJSON_AddItemToArray(education1, cJSON_CreateString("热血")); cJSON_AddItemToArray(education2, cJSON_CreateString("推理")); cJSON_AddItemToArray(education2, cJSON_CreateString("格斗")); // 5.定义 [ ] 数组 /*"languages": { "serialOne": { "language": "汉语", "grade" : 10 }, "serialTwo" : { "language": "英语", "grade" : 6} }*/ cJSON_AddItemToArray(education, education1); cJSON_AddItemToArray(education, education2); // 定义对象 { } cJSON_AddItemToObject(serialOne, "language", cJSON_CreateString("汉语")); cJSON_AddItemToObject(serialOne, "grade", cJSON_CreateNumber(10)); cJSON_AddItemToObject(serialTwo, "language", cJSON_CreateString("英语")); cJSON_AddItemToObject(serialTwo, "grade", cJSON_CreateNumber(6)); // 定义对象 { } cJSON_AddItemToObject(languages, "serialOne", serialOne); cJSON_AddItemToObject(languages, "serialTwo", serialTwo); } void json_write() { //1. 定义对象 { } cJSON* interest = cJSON_CreateObject(); cJSON* final = cJSON_CreateArray(); cJSON* likeObject1 = cJSON_CreateObject(); cJSON* likeObject2 = cJSON_CreateObject(); cJSON* like = cJSON_CreateArray(); cJSON* education1 = cJSON_CreateArray(); cJSON* education2 = cJSON_CreateArray(); cJSON* education = cJSON_CreateArray(); cJSON* serialOne = cJSON_CreateObject(); cJSON* serialTwo = cJSON_CreateObject(); cJSON* languages = cJSON_CreateObject(); cJSON* root = cJSON_CreateObject(); if (interest == nullptr || final == nullptr || likeObject1 == nullptr || likeObject2 == nullptr || like == nullptr || education1 == nullptr || education2 == nullptr || education == nullptr || serialOne == nullptr || serialTwo == nullptr || languages == nullptr || 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); cJSON_Delete(root); return; } AddLikeObject(interest, final, likeObject1, likeObject2, like); AddEducationObject(education1, education2, education, serialOne, serialTwo, languages); // 将子项插入根项中 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); #ifdef TRACK_TEST printf("ALL_JSON:\n%s\n", ALL_JSON); // ALL_JSON:有做格式调整 printf("ALL_JSONUnformatted:\n%s\n", ALL_JSONUnformatted); // cJSON_PrintUnformatted:没有做格式调整 #endif // 返回的字符串指针需要自己释放 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); } int main() { cout << "test CJSON" << "hello" << endl; ArrayToZero arrayToZero; arrayToZero.showArray(); json_write(); return 0; }
cmake_minimum_required(VERSION 3.16.5) message("this is cmakelist log") message(${CMAKE_CURRENT_SOURCE_DIR}) get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME) message(${ProjectId}) message(${ProjectId}) message(NAME) string(REPLACE " " "_" ProjectId ${ProjectId}) message(${ProjectId}) #project(${ProjectId} CXX) project(${ProjectId}) SET(CMAKE_CXX_COMPILER "g++") SET(CMAKE_C_COMPILER "gcc") #添加宏定义Debug为CMAKE_BUILD_TYPE SET(CMAKE_BUILD_TYPE "Release") SET(CMAKE_BUILD_TYPE "Debug") SET(PLATFORM "LINUX") add_definitions(-DCLUSTER_DEBUG) add_definitions(-DCOMBINE_WEEKDAY) set(CMAKE_CXX_STANDARD 17) if (CMAKE_BUILD_TYPE STREQUAL Debug) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -D_DEBUG") else () set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_FORTIFY_SOURCE=2 -O3 -DNDEBUG -s") endif () if (PLATFORM STREQUAL WINDOWS OR PLATFORM STREQUAL windows) SET(PLATFORM windows) add_definitions( -DWIN32 ) else () SET(PLATFORM linux) add_definitions( -Dlinux ) endif() add_definitions( -DTRACK_TEST ) #添加头文件 #例如:include_directories(/usr/include/abc /usr/include/xxx) #是将“/usr/include/abc”和“/usr/include/xxx”这两个目录添加至编译器的头文件搜索目录(两个目录用空格分隔)。 include_directories(../3rd/cJSON) include_directories(../test_memset/include/) #通过编译源文件来创建一个可执行文件,其中,name是生成的可执行文件的名称,source是创建可执行文件所需要的源文件。 #源文件可以使用aux_source_directory指令返回的变量(将源文件保存在这个变量中),也可以是指定的.cpp文件(注意路径)。 aux_source_directory(../3rd/cJSON cJSONSrc) aux_source_directory(../test_memset/src memsetSrc) add_executable(${ProjectId} ${memsetSrc} ${cJSONSrc} main.cpp)
其中需要注意的是,增加下面的语句会出现报错。原因是这里只调用cJSON_Delete(root)就可以了,zeroError,zeroErrorIntersection,finalTime,relatively都挂在了root下面。
// cJSON_Delete(zeroError);
// cJSON_Delete(zeroErrorIntersection);
// cJSON_Delete(finalTime);
// cJSON_Delete(relatively);
然后上述代码,如果删除掉下面的代码好像也不影响,不知道为什么。
#ifdef __cplusplus
extern "C" {
#endif
然后我就在想是不是因为我引用的某个文件已经加了这个定义,然后整个工程就有了这个定义。我在cJSON.h文件中确实发现了上面c++的定义,我现在尝试注释掉这个宏定义,然后再次编译,确实报错了。
所以我现在突然理解了,就是想调用c文件,或者应该这样说,这个c文件想被其他文件调用,就应该在头文件中定义extren “C”,这样其他c++文件就可以正常调用这个c文件。OK!
下面验证cJSON_AddItemToArray(education, education1);这个语句是否是将education1指针挂在在education上,
写测试代码
cJSON_Delete(education1);
cJSON_Delete(education2);
cJSON_Delete(education);
运行程序提示free(): double free detected in tcache 2,所以确实是将education1挂在education上了。只需要释放一次,跟上述root一样处理。
/usr/bin/ld: CMakeFiles/testCJSON.dir/main.cpp.o: in function `json_write()': /data2/z30031397/myProjTest/test_CJSON/main.cpp:15: undefined reference to `cJSON_CreateObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:17: undefined reference to `cJSON_CreateString' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:17: undefined reference to `cJSON_AddItemToObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:18: undefined reference to `cJSON_CreateString' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:18: undefined reference to `cJSON_AddItemToObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:23: undefined reference to `cJSON_CreateArray' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:25: undefined reference to `cJSON_CreateString' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:25: undefined reference to `cJSON_AddItemToArray' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:26: undefined reference to `cJSON_CreateString' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:26: undefined reference to `cJSON_AddItemToArray' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:30: undefined reference to `cJSON_CreateObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:31: undefined reference to `cJSON_CreateString' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:31: undefined reference to `cJSON_AddItemToObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:32: undefined reference to `cJSON_CreateNumber' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:32: undefined reference to `cJSON_AddItemToObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:35: undefined reference to `cJSON_CreateObject' /usr/bin/ld: /data2/z30031397/myProjTest/test_CJSON/main.cpp:36: undefined reference to `cJSON_CreateString'
这边这个问题还堵住了好几天,然后今天再搞一下,发现是因为cpp调用c文件的原因,将Cmakelist.txt文件的CXX删掉就可以了。下次注意这个啦。如果在cmakelist.txt文件中写成了CXX,那么应该是工程定义成C++工程,引用.c文件就会有问题。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。