cJSON 多层json

{
    "Name":    "123",
    "UUID":    "1234",
    "TimeStamp":    252,
    "Sign":    "12365",
    "Mode":    3,
    "Action":    "12396",
    "GetOnePic":    {
        "LibIndex":    9,
        "FlieIndex":    5,
        "Pic":    "1236"
    },
    "TestArray":    [{
            "justTest0":    0
        }, {
            "justTest1":    "12365"
        }, {
            "justTest0":    2
        }, {
            "justTest1":    "12365"
        }, {
            "justTest0":    4
        }]

#include 
#include 
#include 
#include 
#include 
#include 
#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);
	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");
		for(i=0;ivalueint);
				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;
}
 
 

 

你可能感兴趣的:(cJSON 多层json)