在使用cjson的时候遇见这样一个问题(先看问题代码,如下)
void platform_set_matrix_by_udp(cJSON* para, const char* str) {char* chintstr = NULL, * intstr = NULL;cJSON* index, * val;int ch;char* matrix;int number = 0;int rows[8] = { 0 }, cols;char arr[8][8];volatile int matrix_channel = 0;char buffer[80] = { 0 };cJSON* item0 = cJSON_CreateObject();cJSON* item1 = cJSON_CreateObject();cJSON* item2 = cJSON_CreateObject();cJSON* item3 = cJSON_CreateObject();cJSON* item4 = cJSON_CreateObject();cJSON* item5 = cJSON_CreateObject();cJSON* item6 = cJSON_CreateObject();cJSON* item7 = cJSON_CreateObject();cJSON* root = cJSON_CreateObject();cJSON_AddStringToObject(root, "cookie", "0");cJSON* Arr = cJSON_CreateArray();//修改生成inArr或者outArr的情况cJSON_AddItemToObject(root, "arr", Arr);if (strcmp(str, "setMatrix") == 0) {cJSON* setMatrix = cJSON_GetObjectItem(para, "setMatrix");matrix = setMatrix->valuestring;for (int i = 0; i < 8; i++) { // 遍历每一行并输出结果for (int j = 0; j < 8; j++) {arr[i][j] = matrix[i * 8 + j] - '0';}}for (int j = 0; j < 8; j++) { // 遍历每一行并输出结果for (int i = 0; i < 8; i++) {if (arr[i][j] != 0) {matrix_channel = matrix_channel + ((int)pow(2, i));}}switch (j){case 0:chintstr = intToString(0);cJSON_AddStringToObject(item0, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item0, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item0);break;case 1:chintstr = intToString(1);cJSON_AddStringToObject(item1, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item1, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item1);break;case 2:chintstr = intToString(2);cJSON_AddStringToObject(item2, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item2, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item2);break;case 3:chintstr = intToString(3);cJSON_AddStringToObject(item3, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item3, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item3);break;case 4:chintstr = intToString(4);cJSON_AddStringToObject(item4, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item4, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item4);break;case 5:chintstr = intToString(5);cJSON_AddStringToObject(item5, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item5, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item5);break;case 6:chintstr = intToString(6);cJSON_AddStringToObject(item6, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item6, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item6);break;case 7:chintstr = intToString(7);cJSON_AddStringToObject(item7, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item7, "val", buffer);//创建val cJSON_AddItemToArray(Arr, item7);break;default:break;}matrix_channel = 0;}}
}
最后生成数据本来应该是这样:
但在连续几次运行之后,发现不能将index键值对添加到arr数组中,如上图。然后debug的调试下,发现调用cJSON_AddStringToObject(实际上是调用的cJSON_AddItemToObject),在这个函数中会申请新内存,但不知道为什么内存申请失败。于是我将上面的代码改成如下所示:
void platform_set_matrix_by_udp(cJSON* para, const char* str) {char * chintstr = NULL, *intstr = NULL;cJSON *index, *val;err_t err;char* matrix;int number = 0;int rows[8] = {0};char arr[8][8]; volatile int matrix_channel = 0;char buffer[80] = {0};cJSON* item0 = cJSON_CreateObject();cJSON* item1 = cJSON_CreateObject();cJSON* item2 = cJSON_CreateObject();cJSON* item3 = cJSON_CreateObject();cJSON* item4 = cJSON_CreateObject();cJSON* item5 = cJSON_CreateObject();cJSON* item6 = cJSON_CreateObject();cJSON* item7 = cJSON_CreateObject();cJSON *root = cJSON_CreateObject();cJSON_AddStringToObject(root, "cookie", "0");cJSON *Arr = cJSON_CreateArray();if (strcmp(str, "setMatrix") == 0) {cJSON* setMatrix = cJSON_GetObjectItem(para, "setMatrix");matrix = setMatrix->valuestring;for (int i = 0; i < 8; i++) { // 遍历每一行并输出结果for (int j = 0; j < 8; j++) {arr[i][j] = matrix[i * 8 + j] - '0';}}#if 1for (int j = 0; j < 8; j++) { // 遍历每一行并输出结果for (int i = 0; i < 8; i++) {if (arr[i][j] != 0) {matrix_channel = matrix_channel + ((int)pow(2, i));}}switch (j){case 0:cJSON_AddStringToObject(item0, "index", "0");//创建val //cJSON_AddStringToObject(item0, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item0, "val", buffer);//创建val if(cJSON_GetObjectItem(item0, "index") == NULL) {cJSON_AddStringToObject(item0, "index", "0");//创建val }cJSON_AddItemToArray(Arr, item0);break;case 1:cJSON_AddStringToObject(item1, "index", "1");//创建val //cJSON_AddStringToObject(item1, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item1, "val", buffer);//创建val if(cJSON_GetObjectItem(item1, "index") == NULL) {cJSON_AddStringToObject(item1, "index", "1");//创建val }cJSON_AddItemToArray(Arr, item1);break;case 2:cJSON_AddStringToObject(item2, "index", "2");//创建val //cJSON_AddStringToObject(item2, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item2, "val", buffer);//创建val if(cJSON_GetObjectItem(item2, "index") == NULL) {cJSON_AddStringToObject(item2, "index", "2");//创建val }cJSON_AddItemToArray(Arr, item2);break;case 3:cJSON_AddStringToObject(item3, "index", "3");//创建val //cJSON_AddStringToObject(item3, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item3, "val", buffer);//创建val if(cJSON_GetObjectItem(item3, "index") == NULL) {cJSON_AddStringToObject(item3, "index", "3");//创建val }cJSON_AddItemToArray(Arr, item3);break;case 4:cJSON_AddStringToObject(item4, "index", "4");//创建val //cJSON_AddStringToObject(item4, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item4, "val", buffer);//创建val if(cJSON_GetObjectItem(item4, "index") == NULL) {cJSON_AddStringToObject(item4, "index", "4");//创建val }cJSON_AddItemToArray(Arr, item4);break;case 5:cJSON_AddStringToObject(item5, "index", "5");//创建val //cJSON_AddStringToObject(item5, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item5, "val", buffer);//创建val if(cJSON_GetObjectItem(item5, "index") == NULL) {cJSON_AddStringToObject(item5, "index", "5");//创建val }cJSON_AddItemToArray(Arr, item5);break;case 6:cJSON_AddStringToObject(item6, "index", "6");//创建val //cJSON_AddStringToObject(item6, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item6, "val", buffer);//创建val if(cJSON_GetObjectItem(item6, "index") == NULL) {cJSON_AddStringToObject(item6, "index", "6");//创建val }cJSON_AddItemToArray(Arr, item6);break;case 7:cJSON_AddStringToObject(item7, "index", "7");//创建val //cJSON_AddStringToObject(item7, "index", chintstr);//创建indexsprintf(buffer, "0x%X", matrix_channel);cJSON_AddStringToObject(item7, "val", buffer);//创建val if(cJSON_GetObjectItem(item7, "index") == NULL) {cJSON_AddStringToObject(item7, "index", "7");//创建val }cJSON_AddItemToArray(Arr, item7);break;default:break;}matrix_channel = 0;}
也就说将下面这个改成了
chintstr = intToString(7);
cJSON_AddStringToObject(item7, "index", chintstr);//创建index
如下所示的方式:
cJSON_AddStringToObject(item7, "index", "7");//创建index
这样一来居然就能够成功的将index键值对插入到arr数组中。
原因:
我猜测是因为常量字符串不用分配空间,因此在cJSON_AddItemToObject函数中就没有在为字符串申请空间,也就没有申请失败返回,所以能够正常的将index插入到arr数组中。
---------------------以上原因是我猜测的,希望有cjson的前辈能够指点真正的原因是什么-----------