cJson 创建与解析

前端之家收集整理的这篇文章主要介绍了cJson 创建与解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文用代码简单介绍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下编译通过,运行结果如下

1 @H_403_30@{ 2 ": 3 10010@H_403_30@,128); line-height:1.5!important"> 4 ": true@H_403_30@,128); line-height:1.5!important"> 5 "@H_403_30@: { 6 7 @H_403_30@ } 8 @H_403_30@} 9 @H_403_30@obj_1 : hello world 10 obj_2 : 10010 11 obj_3 : 1 12 sub_obj_1 : a sub json string
代码解释如下:

CJSON在内存中的存储方式是用链表进行存储的,所以在进行操作的时候,我们可见的部分全部是用指针进行操作的。

第8行新建一个JSON项目。

第14、15、16行分别添加了字符串、数字和bool变量。

第18行新建一个JSON项目:pSubJson。

第25行在新建的pSubJson项目上添加字符串。

第26行把我们的新项目添加到最初的项目pJsonRoot上。

第28行把CJSON的内存的存储的数据转换为字符串格式。

cjson库的 百度网盘 下载地址在:http://pan.baidu.com/s/1ntsRLgt

结果分析:

第1到8行为创建的JSON字符串

第9到12行为从JSON解析得到的数据

2、创建json数组和解析json数组

1 创建数组,数组值是另一个JSON的item,这里使用数字作为演示 2 char * makeArray(int@H_403_30@ iSize) 3 @H_403_30@{ 4 cJSON * root =@H_403_30@ cJSON_CreateArray(); 5 if(NULL ==@H_403_30@ root) 6 @H_403_30@ { 7 printf(create json array faild\n 8 9 @H_403_30@ } 10 int i = 11 12 for(i = 0; i < iSize; i++@H_403_30@) 13 @H_403_30@ { 14 cJSON_AddNumberToObject(root,0); line-height:1.5!important">hehe15 @H_403_30@ } 16 char * out =@H_403_30@ cJSON_Print(root); 17 @H_403_30@ cJSON_Delete(root); 18 19 return out@H_403_30@; 20 @H_403_30@} 21 22 解析刚刚的CJSON数组 23 void parseArray(char *@H_403_30@ pJson) 24 @H_403_30@{ 25 if(NULL ==@H_403_30@ pJson) 26 @H_403_30@ { 27 28 @H_403_30@ } 29 cJSON * root =@H_403_30@ NULL; 30 if((root = cJSON_Parse(pJson)) ==@H_403_30@ NULL) 31 @H_403_30@ { 32 33 @H_403_30@ } 34 int iSize =@H_403_30@ cJSON_GetArraySize(root); 35 for(int iCnt = 0; iCnt < iSize; iCnt++@H_403_30@) 36 @H_403_30@ { 37 cJSON * pSub =@H_403_30@ cJSON_GetArrayItem(root,iCnt); 38 39 @H_403_30@ { 40 continue@H_403_30@; 41 @H_403_30@ } 42 int iValue = pSub->@H_403_30@valueint; 43 printf(value[%2d] : [%d]\n44 @H_403_30@ } 45 @H_403_30@ cJSON_Delete(root); 46 47 }

猜你在找的Json相关文章