cJson 创建与解析

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

本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据。2)创建json数组和解析json数组

1、创建json,从json中获取数据

  1. 1 #include <st@R_502_410@.h>
  2. 2 #include "cJSON.h"
  3. 3
  4. 4 char * makeJson()
  5. 5 {
  6. 6 cJSON * pJsonRoot = NULL;
  7. 7
  8. 8 pJsonRoot = cJSON_CreateObject();
  9. 9 if(NULL == pJsonRoot)
  10. 10 {
  11. 11 //error happend here
  12. 12 return NULL;
  13. 13 }
  14. 14 cJSON_AddStringToObject(pJsonRoot,hello",0); line-height:1.5!important">hello world");
  15. 15 cJSON_AddNumberToObject(pJsonRoot,0); line-height:1.5!important">number10010);
  16. 16 cJSON_AddBoolToObject(pJsonRoot,0); line-height:1.5!important">bool1);
  17. 17 cJSON * pSubJson = NULL;
  18. 18 pSubJson = cJSON_CreateObject();
  19. 19 if(NULL == pSubJson)
  20. 20 {
  21. 21 create object faild,exit
  22. 22 cJSON_Delete(pJsonRoot);
  23. 23 24 }
  24. 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",pSubJson);
  25. 27
  26. 28 char * p = cJSON_Print(pJsonRoot);
  27. 29 else use :
  28. 30 char * p = cJSON_PrintUnformatted(pJsonRoot);
  29. 31 if(NULL == p)
  30. 32 {
  31. 33 convert json list to string faild,exit
  32. 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
  33. 35 cJSON_Delete(pJsonRoot);
  34. 36 37 }
  35. 38 free(p);
  36. 39
  37. 40 cJSON_Delete(pJsonRoot);
  38. 41
  39. 42 return p;
  40. 43 }
  41. 44
  42. 45 void parseJson(char * pMsg)
  43. 46 {
  44. 47 if(NULL == pMsg)
  45. 48 {
  46. 49 return;
  47. 50 }
  48. 51 cJSON * pJson = cJSON_Parse(pMsg);
  49. 52 if(NULL == pJson)
  50. 53 {
  51. 54 parse faild,return
  52. 55 return ;
  53. 56 }
  54. 57
  55. 58 get string from json
  56. 59 cJSON * pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 60 if(NULL == pSub)
  57. 61 {
  58. 62 get object named "hello" faild
  59. 63 }
  60. 64 printf(obj_1 : %s\nvaluestring);
  61. 65
  62. 66 get number from json
  63. 67 pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 68 69 {
  64. 70 get number from json faild
  65. 71 }
  66. 72 printf(obj_2 : %d\nvalueint);
  67. 73
  68. 74 get bool from json
  69. 75 pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 76 77 {
  70. 78 get bool from json faild
  71. 79 }
  72. 80 printf(obj_3 : %d\n 81
  73. 82 get sub object
  74. 83 pSub = cJSON_GetObjectItem(pJson,128); line-height:1.5!important"> 84 85 {
  75. 86 get sub object faild
  76. 87 }
  77. 88 cJSON * pSubSub = cJSON_GetObjectItem(pSub,128); line-height:1.5!important"> 89 if(NULL == pSubSub)
  78. 90 {
  79. 91 get object from subject object faild
  80. 92 }
  81. 93 printf(sub_obj_1 : %s\nvaluestring);
  82. 94
  83. 95 cJSON_Delete(pJson);
  84. 96 }
  85. 97
  86. 98 int main()
  87. 99 {
  88. 100 char * p = makeJson();
  89. 101 102 {
  90. 103 return 0;
  91. 104 }
  92. 105 printf(%s\n106 parseJson(p);
  93. 107   free(p);  //千万不要忘记释放内存呀,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放
  94. 108 109 }

centos下编译通过,运行结果如下

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

猜你在找的Json相关文章