加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【cJSON】CJSON学习笔记(二)

发布时间:2020-12-16 19:04:55 所属栏目:百科 来源:网络整理
导读:1.重要函数说明 【1】两个创建 【创建JSON对象】cJSON *cJSON_CreateObject(void); 【创建JSON数组】cJSON *cJSON_CreateArray(void); 【2】两种添加 【向对象中添加】void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); 【向数组
1.重要函数说明
【1】两个创建
【创建JSON对象】cJSON *cJSON_CreateObject(void);
【创建JSON数组】cJSON *cJSON_CreateArray(void);

【2】两种添加
【向对象中添加】void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
【向数组中添加】void cJSON_AddItemToArray(cJSON *array,85); line-height:35px; background-color:inherit; font-family:Tahoma; font-size:18px">
【3】常用几招
【向对象中增加数字】cJSON_AddItemToObject(root,"value",cJSON_CreateNumber(value));
【向对象中增加文件】cJSON_AddItemToObject(root,"string",cJSON_CreateString(string));
【4】JSON嵌套
【向对象中增加数组】cJSON_AddItemToObject(root,"rows",rows = cJSON_CreateArray());
【向数组中增加对象】cJSON_AddItemToArray(rows,row = cJSON_CreateObject());
2.创建各种各样的JSON数据包
在这里通过代码举几个例子,更多的内容请查看代码仓库中的相关文件。
【1】JSON数字
[cpp] view plain copy
  1. voidcreate_single_number(void){
  2. cJSON*root;
  3. char*out;
  4. intvalue=24;
  5. root=cJSON_CreateObject();//创建根
  6. cJSON_AddItemToObject(root,"value",cJSON_CreateNumber(value));
  7. //打印并释放
  8. out=cJSON_Print(root);cJSON_Delete(root);printf("%sn",out);free(out);
  9. //控制台输出
  10. #if0
  11. {
  12. "value":24
  13. }
  14. #endif
  15. }
【简单说明】
【1】cJSON_AddItemToObject(root,cJSON_CreateNumber(value));
【2】cJSON_AddNumberToObject(root,value);
【1】和【2】效果完全相同。
【2】JSON字符串
    voidcreate_single_string(char*name="xukai871105";
  1. //方法使用cJSON_AddItemToObject,推荐使用
  2. "name",cJSON_CreateString(name));
  3. "name":"xukai871105"
  4. 【2】 cJSON_AddStringToObject(root,name);
    【3】JSON布尔类型
      voidcreate_bool( root=cJSON_CreateObject();//创建根
    1. cJSON_AddItemToObject(root,"success",cJSON_CreateFalse());
    2. //打印并释放
    3. out=cJSON_Print(root);cJSON_Delete(root);printf("%sn",out);free(out);
    4. //控制台输出
    5. #if0
    6. {
    7. "success":false
    8. }
    9. #endif
    10. }
    【1】布尔类型不需要加引号。
    3.JSON格式嵌套
    JSON格式在使用时往往存在嵌套,例如JSON对象中嵌套JSON数组而JSON数组中嵌套JSON对象,下面就通过几个简单的例子说明问题。
    【1】JSON简单嵌套
      voidcreate_simple(intlist[4]={5,6,7,8};
    1. "lists",cJSON_CreateIntArray(list,4));
    2. "lists":[5,8]
    3. 【2】JSON复杂嵌套
        voidcreate_complex( cJSON*root,*rows,*row;
      1. inti=0;
      2. char*title[3]={"树莓派学习笔记——索引博文",
      3. "树莓派学习笔记——GPIO功能学习",
      4. "物联网学习笔记——索引博文"};
      5. char*url[3]={"http://blog.csdn.net/xukai871105/article/details/23115627",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "http://blog.csdn.net/xukai871105/article/details/12684617",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "http://blog.csdn.net/xukai871105/article/details/23366187"};
      6. cJSON_AddNumberToObject(root,"total",3);
      7. //在object中加入array
      8. "rows",rows=cJSON_CreateArray());
      9. for(i=0;i<3;i++){
      10. //在array中加入object
      11. cJSON_AddItemToArray(rows,row=cJSON_CreateObject());
      12. cJSON_AddItemToObject(row,"title",cJSON_CreateString(title[i]));
      13. cJSON_AddItemToObject(row,"url",cJSON_CreateString(url[i]));
      14. "total":3,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "rows":[{
      15. "title":"树莓派学习笔记——索引博文",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "url":"http://blog.csdn.net/xukai871105/article/details/23115627"
      16. },{
      17. "title":"树莓派学习笔记——GPIO功能学习",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "url":"http://blog.csdn.net/xukai871105/article/details/12684617"
      18. },{
      19. "title":"物联网学习笔记——索引博文",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "url":"http://blog.csdn.net/xukai871105/article/details/23366187"
      20. }]
      21. rows为JSON对象,rows对象中嵌套JSON数组,每一个JSON数组的元素又是一个JSON对象,该该对象名为row,row对象中具有两个键值对,分别是titile和url。

        2015/08/19 17:50

        【其他参考示例】

        1. 函数的使用

        宏的定义原型:
        /* Macros for creating things quickly. */
        #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object,name,cJSON_CreateNull())
        #define cJSON_AddTrueToObject(object,cJSON_CreateTrue())
        #define cJSON_AddFalseToObject(object,cJSON_CreateFalse())
        #define cJSON_AddBoolToObject(object,b) cJSON_AddItemToObject(object,cJSON_CreateBool(b))
        #define cJSON_AddNumberToObject(object,n) cJSON_AddItemToObject(object,cJSON_CreateNumber(n))
        #define cJSON_AddStringToObject(object,s) cJSON_AddItemToObject(object,cJSON_CreateString(s))

        示例:
        cJSON *root = cJSON_CreateObject(); //先创建一个对象
        cJSON_AddNumberToObject(root,123.4);
        cJSON_AddStringToObject(root,"year","2015");
        cJSON_AddNullToObject(root,"secret");
        cJSON_AddTrueToObject(root,"Bool");
        cJSON_AddFalseToObject(root,"false");
        cJSON_AddBoolToObject(root,"Yes",3);
        cJSON_AddBoolToObject(root,0);
        out = cJSON_Print(root); //打印对象
        //out = cJSON_PrintUnformatted(root); //非格式化的打印
        //printf("===> 2: %sn",out);
        printf("%sn",out);
        cJSON_Delete(root); //释放对象所占的内存
        free(out);

        函数的原型:
        /* These calls create a cJSON item of the appropriate type. */
        extern cJSON *cJSON_CreateNull(void);
        extern cJSON *cJSON_CreateTrue(void);
        extern cJSON *cJSON_CreateFalse(void);
        extern cJSON *cJSON_CreateBool(int b);
        extern cJSON *cJSON_CreateNumber(double num);
        extern cJSON *cJSON_CreateString(const char *string);
        extern cJSON *cJSON_CreateArray(void);
        extern cJSON *cJSON_CreateObject(void);

        示例:参考上面博客的代码

        函数原型:
        /* These utilities create an Array of count items. */
        extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
        extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
        extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
        extern cJSON *cJSON_CreateStringArray(const char **strings,int count);

        /* Append item to the specified array/object. */
        extern void cJSON_AddItemToArray(cJSON *array,cJSON *item);
        extern void cJSON_AddItemToObject(cJSON *object,cJSON *item);
        extern void cJSON_AddItemToObjectCS(cJSON *object,cJSON *item);

        示例:
        char *out = NULL;
        int array[] = {1,2,3,4};
        int array1[] = {1,4,5,6};
        cJSON *json = NULL;
        cJSON *item = NULL;
        cJSON *tmp = NULL;

        json = cJSON_CreateObject();
        //cJSON_AddItemToObject(json,"array",cJSON_CreateIntArray(array,4)); //与下面两行等价
        item = cJSON_CreateIntArray(array,4);
        cJSON_AddItemToObject(json,item);
        out = cJSON_Print(json);
        printf("%sn",out);

        printf("=========================n");
        printf("size: %dn",cJSON_GetArraySize(item)); //输出为4 4个元素

        tmp = cJSON_GetArrayItem(json,0); //获取json中的第一个节点
        if (NULL != tmp)
        {
        printf("ok..n");
        printf("size: %dn",cJSON_GetArraySize(tmp));
        }
        else
        printf("error...n");

        //此时item数组中的元素有 1 2 3 4 5 6 7
        printf("=========================n");
        cJSON_AddItemToArray(item,cJSON_CreateNumber(5));
        cJSON_AddItemToArray(item,cJSON_CreateNumber(6));
        cJSON_AddItemToArray(item,cJSON_CreateNumber(7));
        printf("size: %dn",cJSON_GetArraySize(item));
        out = cJSON_Print(json);
        printf("%sn",out);

        tmp = cJSON_CreateIntArray(array1,6);
        //cJSON_AddItemToObject(json,"array1",tmp); //好像与下面一句话等价呢
        cJSON_AddItemReferenceToObject(json,tmp);
        out = cJSON_Print(json);
        printf("%sn",out);


        //整形数组的参考示例
        int array3[] = {1,9};
        json = cJSON_CreateIntArray(array3,5);
        out = cJSON_Print(json); cJSON_Delete(json); printf("%sn",out); free(out);

        //字符指针数组
        const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        json = cJSON_CreateStringArray(strings,7);
        out = cJSON_Print(json); cJSON_Delete(json); printf("%sn",out); free(out);


        参考博客:http://blog.csdn.net/xukai871105/article/details/33013455

        (编辑:李大同)

        【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读