cJson库的使用

来源:互联网 发布:淘宝代码需要修改热点 编辑:程序博客网 时间:2024/04/29 16:49

本文出自 : http://stevengw.blog.163.com/blog/static/5834475120124643458695/

                     http://diaorui.net/?p=245


cJSON简介:

JSON(JavaScriptObject Notation)是一种轻量级的数据交换格式。它基于JavaScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。

cJSON一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。

cJSON结构体:

typedefstruct cJSON {

structcJSON *next,*prev;

struct cJSON *child;

int type;

char * valuestring;

int valueint;

double valuedouble;

char *string;

}cJSON;

1cJSON存储的时候是采用链表存储的,其访问方式很像一颗树。每一个节点可以有兄妹节点,通过next/prev指针来查找,它类似双向链表;每个节点也可以有孩子节点,通过child指针来访问,进入下一层。

不过,只有节点是对象或数组才可以有孩子节点。

2type一共有7种取值,分别是:

#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

若是Number类型,则valueintvaluedouble中存储着值,若你期望的是int,则访问valueint,若期望的是double,则访问valuedouble,可以得到值。

若是String类型的,则valuestring中存储着值,可以访问valuestring得到值。

3string中存放的是这个节点的名字。

用法:

1、只需在函数中includecJSON.h头文件,然后和cJSON.c或库文件libcJSON.a一起编译即可使用。

2、具体函数用法详见cJSON.h中注释

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

http://apps.hi.baidu.com/share/detail/42828576

c语言解析json数据

c语言解析json数据
文章分类:C++编程 
我使用的是cJSON:http://sourceforge.net/projects/cjson/

先看json的数据结构 
c中没有对象,所以json数据是采用链表存储的 
C代码  
typedef struct cJSON {   
    struct cJSON *next,*prev;   // 数组 对象数据中用到   
    struct cJSON *child;        // 数组 和对象中指向子数组对象或值   
  
    int type;           // 元素的类型,如是对象还是数组   
  
    char *valuestring;          // 如果是字符串   
    int valueint;               // 如果是数值   
    double valuedouble;         // 如果类型是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; 

typedef struct cJSON {
 struct cJSON *next,*prev; // 数组 对象数据中用到
 struct cJSON *child;  // 数组 和对象中指向子数组对象或值

 int type;   // 元素的类型,如是对象还是数组

 char *valuestring;   // 如果是字符串
 int valueint;    // 如果是数值
 double valuedouble;   // 如果类型是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;

比如你有一个json数据 
Javascript代码  
{   
    "name": "Jack (\"Bee\") Nimble",    
    "format": {   
        "type":       "rect",    
        "width":      1920,    
        "height":     1080,    
        "interlace":  false,    
        "frame rate": 24   
    }   

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

那么你可以 
1:讲字符串解析成json结构体。 
C代码  
cJSON *root = cJSON_Parse(my_json_string); 

cJSON *root = cJSON_Parse(my_json_string);
2:获取某个元素 
C代码  
cJSON *format = cJSON_GetObjectItem(root,"format");   
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; 

cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
3:讲json结构体转换成字符串 
C代码  
char *rendered=cJSON_Print(root); 

char *rendered=cJSON_Print(root);
4:删除 
C代码  
cJSON_Delete(root); 

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


extern cJSON *cJSON_Parse(const char *value);//解析一个json字符串为cJSON对象
extern char  *cJSON_Print(cJSON *item);//将json对象转换成容易让人看清结构的字符串
extern char  *cJSON_PrintUnformatted(cJSON *item);//将json对象转换成一个很短的字符串,无回车
extern void   cJSON_Delete(cJSON *c);//删除json对象
extern int  cJSON_GetArraySize(cJSON *array);//返回json数组大小
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);//返回json数组中指定位置对象
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);//返回指定字符串对应的json对象
extern cJSON *cJSON_CreateIntArray(int *numbers,int count);//生成整型数组json对象
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);//向数组中添加元素