在CJSON的开发中,对于新⼿来说遇到的问题⼀般是或许如下⼏个: 第⼀、如何将C结构体转为⼀个Json数据格式的结构体?
第⼆、如果遇到Json数据格式中含有结构体数组怎么解析?
第三、如果遇到多层结构体嵌套⼜怎么解析?
如下图⽰,为多层结构体嵌套以及内含⼀个结构体数组:
以上问题也是我在进⾏JSON开发时所遇到的问题,经过学习及实验,最后才将CJSON的应⽤熟练,以下是代码的整理,对于多层结构体嵌套,嵌套的结构体数组如何添加和解析等的使⽤⽅法,在此记录只为供交流学习、复习及⽇后参考。
注意:在创建对象的时候要释放创建出来的对象空间,如果内嵌多个对象,只需要delete最顶层的root就⾏了。 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include "cJSON.h"
struct stGetOnePic{
int LibIndex;
外语与外语教学int FlieIndex;
char Pic[16];
};
struct stTestArray{
int justTest0;
char justTest1[16];
};
struct stInfo{
char Name[8];
char UUID[16];
int TimeStamp;
char Sign[16];
int Mode;
char Action[16];
struct stGetOnePic GetOnePic;
struct stTestArray TestArray[5];
};
int main()
{
int i =0;
/
/鐢熸垚Json鏁版嵁鏍煎紡
cJSON*response_root = cJSON_CreateObject();
cJSON_AddStringToObject(response_root, "Name", "123");
cJSON_AddStringToObject(response_root, "UUID", "1234");
cJSON_AddNumberToObject(response_root, "TimeStamp", 252);
56个民族简介cJSON_AddStringToObject(response_root, "Sign", "12365");
cJSON_AddNumberToObject(response_root, "Mode", 3);
网络拓扑图cJSON_AddStringToObject(response_root, "Action", "12396");
cJSON*getonepic_root = cJSON_CreateObject();
cJSON_AddNumberToObject(getonepic_root, "LibIndex", 9);
cJSON_AddNumberToObject(getonepic_root, "FlieIndex", 5);
cJSON_AddStringToObject(getonepic_root, "Pic", "1236");
cJSON_AddItemToObject(response_root, "GetOnePic", getonepic_root);
cJSON*array=cJSON_CreateArray();
cJSON_AddItemToObject(response_root,"TestArray",array);
for(i=0;i<5;i++)
高频整流器
{
cJSON*obj=cJSON_CreateObject();
cJSON_AddItemToArray(array,obj);
if(i%2 == 0) cJSON_AddNumberToObject(obj, "justTest0", i);
else cJSON_AddStringToObject(obj, "justTest1", "12365");
有源滤波器}
char *response_str = cJSON_Print(response_root);
printf("%s\n",response_str);
cJSON *pRcvJson = cJSON_Parse(response_str);
if(NULL == pRcvJson){
printf("%s cJSON_Parse failed, line =%ld\n",__FUNCTION__, __LINE__); return -1;
}
cJSON *item=cJSON_GetObjectItem(pRcvJson,"Name");
if(item!=NULL) printf("Name:%s\n",item->valuestring);
item=cJSON_GetObjectItem(pRcvJson,"UUID");
if(item!=NULL) printf("UUID:%s\n",item->valuestring);
item=cJSON_GetObjectItem(pRcvJson,"TimeStamp");
if(item!=NULL) printf("TimeStamp:%u\n",item->valueint);
item=cJSON_GetObjectItem(pRcvJson,"Sign");
if(item!=NULL) printf("Sign:%s\n",item->valuestring);
item=cJSON_GetObjectItem(pRcvJson,"Mode");
if(item!=NULL) printf("Mode:%d\n",item->valueint);
item=cJSON_GetObjectItem(pRcvJson,"Action");
if(item!=NULL) printf("Action:%s\n",item->valuestring);
cJSON* psecond = cJSON_GetObjectItem(pRcvJson,"GetOnePic");
if(psecond!=NULL)
{
item=cJSON_GetObjectItem(psecond,"LibIndex");
if(item!=NULL) printf("LibIndex:%d\n",item->valueint);
item=cJSON_GetObjectItem(psecond,"FlieIndex");
if(item!=NULL) printf("FlieIndex:%d\n",item->valueint);
item=cJSON_GetObjectItem(psecond,"Pic");
if(item!=NULL) printf("Pic:%s\n",item->valuestring);
}
cJSON *arrayread=cJSON_GetObjectItem(pRcvJson,"TestArray");
if(arrayread !=NULL)
{
int arraysize=cJSON_GetArraySize(arrayread);
printf("TestArray:\n");
printf("TestArray:\n");
for(i=0;i<arraysize;i++)
{
item=cJSON_GetArrayItem(arrayread,i);
if(item != NULL)
{
cJSON *itemread=cJSON_GetObjectItem(item,"justTest0");
if(itemread != NULL) printf("justTest0:%d\n",itemread->valueint);
itemread=cJSON_GetObjectItem(item,"justTest1");
if(itemread != NULL) printf("justTest1:%s\n",itemread->valuestring);
}
}
}
if(response_root!=NULL) cJSON_Delete(response_root); //在这⾥释放最顶层的就好 if(pRcvJson !=NULL) cJSON_Delete(pRcvJson);
free(response_str);
return 0;
}
内蒙古 大学