微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cjson源码学习

cjson源代码下载地址

https://sourceforge.net/projects/cjson/?source=directory

主要是cJSON.h,cJSON.c,test.c三个文件

json结构

json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构。
1. 对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,…}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。
2. 数组:数组在js中是中括号“[]”括起来的内容,数据结构为 [“java”,”javascript”,”vb”,…],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。
经过对象、数组2种结构就可以组合成复杂的数据结构了。

JSON 值:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在方括号中)
  • 对象(在花括号中)
  • null

json的详细语法,规则可自行搜索

josn.cn学习地址: http://json.cn/

/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
/* 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结构,结构体存放的相关内容
* 双向链表 用来描述数组或对象
* 孩子cjson结构
* 类型
* 值

http://json.cn/

{
“name”: “Jack (“Bee”) Nimble”,
“format”: {
“type”: “rect”,
“width”: 1920,
“height”: 1080,
“interlace”: false,
“frame rate”: 24
}
}

直接debug源码中的test.c,一步步的运行下去,查看该过程和其中的结构体

一个要转换的字符串如下:

char text1[] = "{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\",\n\"format\": {\"type\":       \"rect\",\n\"width\":      1920,\n\"height\":     1080,\n\"interlace\":  false,\"frame rate\": 24\n}\n}";

主要是将字符串转换成cJSON结构体对象,这里有装换成字符串,数组,对象等等方法
根{,[,”等json规定格式的字符去判断,然后一步步的装换

/* Parser core - when encountering text,process appropriately. */
static const char *parse_value(cJSON *item,const char *value)
{
    if (!value)                     return 0;   /* Fail on null. */
    if (!strncmp(value,"null",4)) { item->type = cJSON_NULL;  return value + 4; }
    if (!strncmp(value,"false",5))    { item->type = cJSON_False; return value + 5; }
    if (!strncmp(value,"true",4)) { item->type = cJSON_True; item->valueint = 1;  return value + 4; }
    if (*value == '\"')             { return parse_string(item,value); }
    if (*value == '-' || (*value >= '0' && *value <= '9'))  { return parse_number(item,value); }
    if (*value == '[')              { return parse_array(item,value); }
    if (*value == '{')              { return parse_object(item,value); }

    ep = value; return 0;   /* failure. */
}

下面是debug过程中的截图,其中有需要经常处理里面的空格等无效字符函数skip(),然后是具体对象里面的各个转换函数调用(parse_xx())

parse_sting函数只要是处理了各种编码的字符串,分配了内存,赋值给cjson* item对象
unicode编码等实现和转换工具可以自行搜索

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐