cJSON剖析一

cJSON:
1.what;
2.how;
3.when;
4.why;

1.what:
          JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
          JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括<a target=_blank target="_blank" href="http://baike.baidu.com/subview/10075/6770152.htm">C</a>、C++、<a target=_blank target="_blank" href="http://baike.baidu.com/view/6590.htm">C#</a>、<a target=_blank target="_blank" href="http://baike.baidu.com/subview/29/12654100.htm">Java</a>、JavaScript、<a target=_blank target="_blank" href="http://baike.baidu.com/view/46614.htm">Perl</a>、<a target=_blank target="_blank" href="http://baike.baidu.com/view/21087.htm">Python</a>等)。
          这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
                                                                                                                                                                                                 ---------come from  baidu
         cJSON 是一个用c语言完成的JSON解释器。
2.how:
        实际上github上的代码已经容易理解怎么使用了,make之后看到的编译过程,或是直接看Makefile文件,
        我们只需要把我们的字符串传入doit()函数即可,这样就可以将字符串转变成结构体的成员。
3.when:
       when we need a JSON;
4.
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==<span style="color:#FF0000;">cJSON_String</span> */
    int valueint;                /* The item's number, if type==<span style="color:#FF0000;">cJSON_Number</span> */
    double valuedouble;            /* The item's number, if type==<span style="color:#FF0000;">cJSON_Number</span> */

    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_String,什么是 cJSON_Number
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

过程:
下面的函数见名只意:
static const char *parse_value(cJSON *item,const char *value);
static const char *parse_array(cJSON *item,const char *value);
static const char *parse_object(cJSON *item,const char *value);
static const char *parse_number(cJSON *item,const char *num);
static const char *parse_string(cJSON *item,const char *str);
static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}//跳过空白字符或是控制符
cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}

cJSON_Parse:
    cJSON_ParseWithOpts:

        parse_value:
           parse_number|parse_string|parse_array|parse_object
               
parse_number: nothing
parse_string: nothing
parse_array :
    parse_value

parse_object:
    circle:
    parse_string
 一般我们是从array,object 开始,对于array,直接进入parse_value,于是就进入了选择,判断是什么类型的数据,分别进入不同的分支。
如果一开始就是object,那么进入parse_string parse_string,从而完成数据格式的转化。
对于数据的存储,这里采用了双链表加上一个孩子域,不同的数据类型有不同的标示,OK;

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值