cJson庫的使用

本文出自 : 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);//向數組中添加元素





發佈了4 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章