本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据。2)创建json数组和解析json数组
1、创建json,从json中获取数据
1 #include <stdio.h> 2 #include "cJSON.h" 3 4 char *@H_403_30@ makeJson() 5 @H_403_30@{ 6 cJSON * pJsonRoot =@H_403_30@ NULL; 7 8 pJsonRoot =@H_403_30@ cJSON_CreateObject(); 9 if(NULL ==@H_403_30@ pJsonRoot) 10 @H_403_30@ { 11 //error happend here 12 return@H_403_30@ NULL; 13 @H_403_30@ } 14 cJSON_AddStringToObject(pJsonRoot,hello",0); line-height:1.5!important">hello world"@H_403_30@); 15 cJSON_AddNumberToObject(pJsonRoot,0); line-height:1.5!important">number10010@H_403_30@); 16 cJSON_AddBoolToObject(pJsonRoot,0); line-height:1.5!important">bool1@H_403_30@); 17 cJSON * pSubJson =@H_403_30@ NULL; 18 pSubJson =@H_403_30@ cJSON_CreateObject(); 19 if(NULL ==@H_403_30@ pSubJson) 20 @H_403_30@ { 21 create object faild,exit 22 @H_403_30@ cJSON_Delete(pJsonRoot); 23 24 @H_403_30@ } 25 cJSON_AddStringToObject(pSubJson,0); line-height:1.5!important">subjsonobja sub json string 26 cJSON_AddItemToObject(pJsonRoot,0); line-height:1.5!important">subobj"@H_403_30@,pSubJson); 27 28 char * p =@H_403_30@ cJSON_Print(pJsonRoot); 29 else use : 30 char * p = cJSON_PrintUnformatted(pJsonRoot); 31 if(NULL ==@H_403_30@ p) 32 @H_403_30@ { 33 convert json list to string faild,exit 34 because sub json pSubJson han been add to pJsonRoot,so just delete pJsonRoot,if you also delete pSubJson,it will coredump,and error is : double free 35 @H_403_30@ cJSON_Delete(pJsonRoot); 36 37 @H_403_30@ } 38 free(p); 39 40 @H_403_30@ cJSON_Delete(pJsonRoot); 41 42 return@H_403_30@ p; 43 @H_403_30@} 44 45 void parseJson(char *@H_403_30@ pMsg) 46 @H_403_30@{ 47 if(NULL ==@H_403_30@ pMsg) 48 @H_403_30@ { 49 return@H_403_30@; 50 @H_403_30@ } 51 cJSON * pJson =@H_403_30@ cJSON_Parse(pMsg); 52 if(NULL ==@H_403_30@ pJson) 53 @H_403_30@ { 54 parse faild,return 55 return@H_403_30@ ; 56 @H_403_30@ } 57 58 get string from json 59 cJSON * pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 60 if(NULL ==@H_403_30@ pSub) 61 @H_403_30@ { 62 get object named "hello" faild 63 @H_403_30@ } 64 printf(obj_1 : %s\n403_30@valuestring); 65 66 get number from json 67 pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 68 69 @H_403_30@ { 70 get number from json faild 71 @H_403_30@ } 72 printf(obj_2 : %d\n403_30@valueint); 73 74 get bool from json 75 pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 76 77 @H_403_30@ { 78 get bool from json faild 79 @H_403_30@ } 80 printf(obj_3 : %d\n 81 82 get sub object 83 pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 84 85 @H_403_30@ { 86 get sub object faild 87 @H_403_30@ } 88 cJSON * pSubSub = cJSON_GetObjectItem(pSub,128); line-height:1.5!important"> 89 if(NULL ==@H_403_30@ pSubSub) 90 @H_403_30@ { 91 get object from subject object faild 92 @H_403_30@ } 93 printf(sub_obj_1 : %s\n403_30@valuestring); 94 95 @H_403_30@ cJSON_Delete(pJson); 96 @H_403_30@} 97 98 int@H_403_30@ main() 99 @H_403_30@{ 100 char * p =@H_403_30@ makeJson(); 101 102 @H_403_30@ { 103 return 0@H_403_30@; 104 @H_403_30@ } 105 printf(%s\n106 @H_403_30@ parseJson(p); 107 free(p); //千万不要忘记释放内存呀,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放 108 109 }
centos下编译通过,运行结果如下