CJSON源码研究笔记

参考:http://blog.csdn.net/coding__madman/article/details/51304093
参考:http://blog.csdn.net/lintax/article/details/50993958
参考:https://www.cnblogs.com/chineseboy/p/3959852.html

static void *(*cJSON_malloc)(size_t sz) = malloc;//定义一个函数指针,并初始化指向malloc函数
static void (*cJSON_free)(void *ptr) = free;//定义一个函数指,针并初始化指向free函数
typedef struct cJSON
{
  struct cJSON *next, *prev; 
  struct cJSON *child;       

  int type;            //解析的时候用到

  char *valuestring;   //值string   
  int valueint;        //值int       
  double valuedouble;  //值double    

  char *string;        //键   键:值   键只有一种,值确有4中:string/int/double/object
} cJSON;

实战


  • 第三方库
    • cJSON.h
    • cJOSN.c
    • 下载链接:链接:http://pan.baidu.com/s/1miKeVDu 密码:y7b4
//编译时要加上 -lm
#include 
#include 
#include 
#include "cJSON.h"

//拼装
int create_js(void)
{
#if 0
    cJSON *root;
    root = cJSON_CreateObject();
    if (!root)
    {
        printf("get root faild !\n");
        return 0;
    }


    cJSON *js_body;

    const char *const body = "body";
    cJSON_AddItemToObject(root, body, js_body = cJSON_CreateObject());
    cJSON_AddStringToObject(js_body, "name", "xiaohui");
    cJSON_AddNumberToObject(js_body, "value", 600);

    char *s = cJSON_PrintUnformatted(root);
    if (s)
    {
        printf("%s\n", s);
        free(s);
        s = NULL;
    }

    if(root)
    {
        cJSON_Delete(root);
        root = NULL;
    }


    return 1;

#endif

#if 0
    cJSON *root,*js_body;
    root = cJSON_CreateArray();
    cJSON_AddItemToArray(root,cJSON_CreateString("hello world"));
    cJSON_AddItemToArray(root,cJSON_CreateNumber(10));

    char *s = cJSON_Print(root);
    char *ss = cJSON_PrintUnformatted(root);
    if(s)
    {
        printf("%s",s);
        printf("%s",ss);
        free(s);
        free(ss);
        s = ss = NULL;
    }
    if(root)
    {
        cJSON_Delete(root);
        root = NULL;
    }
    return 1;
#endif

#if 0
    cJSON *root,*js_body,*js_list;
    root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"body",js_body = cJSON_CreateArray());
    cJSON_AddItemToArray(js_body,js_list = cJSON_CreateObject());
    cJSON_AddStringToObject(js_list,"name","xiaohong");
    cJSON_AddNumberToObject(js_list,"status",100);



    char *s = cJSON_PrintUnformatted(root);
    if(s)
    {
        printf("%s\n",s);        
        free(s);
        s = NULL;
    }
    if(root)
    {        
        cJSON_Delete(root);        
        root = NULL;        
    }
    return 1;

#endif
}

//解析
int parse_js(void)
{
#if 1
    //{"list":[{"name":"xiaohong","age":10},{"name":"huahua","age":11}]}
    char *js_string = "{\"list\":[{\"name\":\"xiaohong\",\"age\":10},{\"name\":\"huahua\",\"age\":11}]}";
    cJSON *root = cJSON_Parse(js_string);
    cJSON *js_list = cJSON_GetObjectItem(root, "list");
    printf("js_list type is %d\n",js_list->type);
    int array_size = cJSON_GetArraySize(js_list);
    printf("array size is %d\n", array_size);
    int i = 0;
    cJSON *item;
    cJSON *it;
    for (i = 0; i < array_size; ++i)
    {
        item = cJSON_GetArrayItem(js_list,i);
        printf("item type is %d\n",item->type);
        it = cJSON_GetObjectItem(item,"name");
        printf("it type is %d\n",it->type);
        printf("it value is %s\n",it->valuestring);
        it = cJSON_GetObjectItem(item,"age");
        printf("it type is %d\n",it->type);
        printf("it value is %d\n",it->valueint);
    }
    if(root)
    {
        cJSON_Delete(root);
        root = NULL;
    }
    return 1;

#endif

#if 0
    //{"list":["name1","name2"]}
    char *js_string = "{\"list\":[\"name1\",\"mame2\"]}";
    cJSON *root = cJSON_Parse(js_string);
    cJSON *js_list = cJSON_GetObjectItem(root,"list");
    printf("js_list type is %d\n",js_list->type);
    int array_size = cJSON_GetArraySize(js_list);
    printf("array size is %d\n",array_size);
    int i = 0;
    cJSON *item;
    for(i = 0;i < array_size;++i)
    {
        item = cJSON_GetArrayItem(js_list,i);
        printf("item type is %d\n",item->type);
        printf("%s\n",item->valuestring);
    }

    if(root)
    {
        cJSON_Delete(root);
        root = NULL;
    }
    return 0;

#endif

#if 0
    char *js_string = "{\"list\":{\"name\":\"xiaohong\",\"age\":10},\"other\":{\"name\":\"huahua\"}}";
    cJSON *root = cJSON_Parse(js_string);

    cJSON *js_list = cJSON_GetObjectItem(root,"list");
    printf("list type is %d\n",js_list->type);
    cJSON *name = cJSON_GetObjectItem(js_list,"name");
    printf("name type is %d\n",name->type);
    printf("name is %s\n",name->valuestring);
    cJSON *age = cJSON_GetObjectItem(js_list,"age");
    printf("age type is %d\n",age->type);
    printf("age is %d\n",age->valueint);

    cJSON *js_other = cJSON_GetObjectItem(root,"other");
    printf("other type is %d\n",js_other->type);
    cJSON *js_name = cJSON_GetObjectItem(js_list,"name");
    printf("js_name type is %d\n",js_name->type);
    printf("js_name is %s\n",js_name->valuestring);

    if(root)
    {
        cJSON_Delete(root);
        root = NULL;
    }
    return 0;

#endif

#if 0
    char *js_string = "{\"name\":\"xiao hong\",\"age\":10}";
    //{"name":"xiao hong","age":10}    
    cJSON *root;
    root  = cJSON_Parse(js_string);
    cJSON *name = cJSON_GetObjectItem(root,"name");    
    printf("name type is %d\n",name->type);
    printf("name is %s\n",name->valuestring);

    cJSON *age = cJSON_GetObjectItem(root,"age");
    printf("age type is %d\n",age->type);
    printf("age is %d\n",age->valueint);





    if(root)
    {
        cJSON_Delete(root);
        root = NULL;
    }

#endif
}

int main(void)
{
    create_js();//拼装

    parse_js(); //解析

    return 0;
}

你可能感兴趣的:(cjson,cjson)