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

cJSON使用

发布时间:2020-12-16 19:41:59 所属栏目:百科 来源:网络整理
导读:结合网上cJSON的一些资料,博客,略微方便入门一些。 先看json的数据结构c中没有对象,所以json数据是采用链表存储的 [cpp] view plaincopyprint? typedef struct cJSON{ struct cJSON*next,*prev; //数组对象数据中用到 struct cJSON*child; //数组和对象中

结合网上cJSON的一些资料,博客,略微方便入门一些。

先看json的数据结构c中没有对象,所以json数据是采用链表存储的
[cpp]view plaincopyprint?
  1. typedefstructcJSON{
  2. structcJSON*next,*prev;//数组对象数据中用到
  3. structcJSON*child;//数组和对象中指向子数组对象或值
  4. inttype;//元素的类型,如是对象还是数组
  5. char*valuestring;//如果是字符串
  6. intvalueint;//如果是数值
  7. doublevaluedouble;//如果类型是cJSON_Number
  8. char*string;//Theitem'snamestring,ifthisitemisthechildof,orisinthelistofsubitemsofanobject.
  9. }cJSON;


比如你有一个json数据
[javascript]
    {
  1. "name":"Jack("Bee")Nimble",
  2. "format":{
  3. "type":"rect",226)"> "width":1920,
  4. "height":1080,226)"> "interlace":false,226)"> "framerate":24
  5. }
  6. }

1:讲字符串解析成json结构体。

C代码
  1. cJSON*root=cJSON_Parse(my_json_string);

2:获取某个元素
C代码 cJSON*format=cJSON_GetObjectItem(root,"format");
  • intframerate=cJSON_GetObjectItem(format,"framerate")->valueint;

  • 3:讲json结构体转换成字符串
    C代码char*rendered=cJSON_Print(root);

    4:删除
    C代码 cJSON_Delete(root);

    5:构建一个json结构体
    C代码 cJSON*root,*fmt;
  • root=cJSON_CreateObject();
  • cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack("Bee")Nimble"));
  • "format",fmt=cJSON_CreateObject());
  • cJSON_AddStringToObject(fmt,"type","rect");
  • cJSON_AddNumberToObject(fmt,"width",1920);
  • "height",1080);
  • cJSON_AddFalseToObject(fmt,"interlace");
  • "framerate",24);
  • 6:创建如下

    root:[{"name":"aaa","num":14},{"type":"int",id:123},{...}]

    json是键值对,一个键对应一个值,这里的root对应的是一个数组,这里数组成员是对象,也可以是其它的。但是注意这里的数组的成员可以不同。


    1)cJSON *root,*array;//声明对象
    2)array=cJSON_CreateArray();//创建cJSON数组
    3)root=cJSON_CreateObject();//创建cJSON对象
    4)cJSON_AddItemToObject(root,"name",cJSON_CreateString("aaa"));
    5)cJSON_AddItemToObject(root,"no",cJSON_CreateNumber(1));
    6)cJSON_AddItemToArray(array,root);
    重复3-6步,往对象中加数据可以不同,将往数组添加多条记录


    7.下面把转来的代码贴上。

    /*******************************************

    *先C转成JSON的字符串,然后再把这个JSON的字符串转回来。

    *******************************************/

    #include "stdio.h"

    #include "cjson.h"

    /*******************************


    int main_()

    {

      //首先是用C转换成JSON
    char *out ;
    cJSON *root,*fmt;
    root=cJSON_CreateObject();//创建项目
    cJSON_AddItemToObject(root,cJSON_CreateString("Jack ("Bee") Nimble"));
    cJSON_AddItemToObject(root,"format",fmt=cJSON_CreateObject());//在项目上添加项目
    cJSON_AddStringToObject(fmt,"type","rect");//在项目上的项目上添加字符串,这说明cJSON是可以嵌套的
    cJSON_AddNumberToObject(fmt,"width",1920);
    cJSON_AddNumberToObject(fmt,"height",1080);
    cJSON_AddNumberToObject(fmt,"frame rate",24);

    out=cJSON_Print(fmt);
    printf("%sn",out);//此时out指向的字符串就是JSON格式的了
    free(out);//释放空间

      //接下来进行JSON格式向回转换

      cJSON *fmt = NULL,*JSONroot = NULL;

      num = cJSON_GetArraySize(JSONroot);//看看有多少个项目

      fmt = cJSON_GetObjectItem(JSONroot,"name");

      char name[256];

      snprintf(name,256,"%s",fmt->valuestring);//把fmt指向的JSON节点的字符串复制到name数组里来。

                            //JSON是采用链式存储的,就是链表存储。具体的结构体可以在"cjson.h"里面找到

      /* The cJSON structure: */
    //typedef struct cJSON {
    // struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively,use GetArraySize/GetArrayItem/GetObjectItem */
    // struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

    // int type; /* The type of the item,as above. */

    // char *valuestring; /* The item's string,if type==cJSON_String */
    // int valueint; /* The item's number,if type==cJSON_Number */
    // double valuedouble; /* The item's number,if type==cJSON_Number */

    // char *string; /* The item's name string,if this item is the child of,or is in the list of subitems of an object. */
    //} cJSON;

      cJSON *child;

      child = cJSON_GetObjectItem(fmt,"type");

      char type[256];

      snprintf(type,child->valuestring);

      int width = child->valueint;

      int heigh = child->valueint;

      int frame = child->valueint;

    return 0; }

    (编辑:李大同)

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

      推荐文章
        热点阅读