超輕JSON解析器

原文鏈接

http://blog.csdn.net/itisyang/article/details/53190850


cJSON源碼

https://github.com/DaveGamble/cJSON

JSON介紹

http://www.json.org/json-zh.html


cJSON源文件僅 

cJSON.c

cJSON.h



json的數據結構
  1. typedef struct cJSON {  
  2.     struct cJSON *next,*prev;   // 數組 對象數據中用到  
  3.     struct cJSON *child;        // 數組 和對象中指向子數組對象或值  
  4.   
  5.     int type;           // 元素的類型,如是對象還是數組  
  6.   
  7.     char *valuestring;          // 如果是字符串  
  8.     int valueint;               // 如果是數值  
  9.     double valuedouble;         // 如果類型是cJSON_Number  
  10.   
  11.     char *string;               // “鍵”的名稱 
  12. } cJSON;  

json數據 
  1. {  
  2.     "name""Jack (\"Bee\") Nimble",   
  3.     "format": {  
  4.         "type":       "rect",   
  5.         "width":      1920,   
  6.         "height":     1080,   
  7.         "interlace":  false,   
  8.         "frame rate": 24  
  9.     }  


字符串解析成json結構體
  1. cJSON *root = cJSON_Parse(my_json_string);  

獲取某個元素 
  1. cJSON *format = cJSON_GetObjectItem(root,"format");  
  2. int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;  

json結構體轉換成字符串 
  1. char *rendered=cJSON_Print(root);  

刪除 
  1. cJSON_Delete(root);  

構建一個json結構體 
  1. cJSON *root,*fmt;  
  2. root=cJSON_CreateObject();    
  3. cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));  
  4. cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());  
  5. cJSON_AddStringToObject(fmt,"type",     "rect");  
  6. cJSON_AddNumberToObject(fmt,"width",        1920);  
  7. cJSON_AddNumberToObject(fmt,"height",       1080);  
  8. cJSON_AddFalseToObject (fmt,"interlace");  
  9. cJSON_AddNumberToObject(fmt,"frame rate",   24);  

json的數據結構
  1. typedef struct cJSON {  
  2.     struct cJSON *next,*prev;   // 數組 對象數據中用到  
  3.     struct cJSON *child;        // 數組 和對象中指向子數組對象或值  
  4.   
  5.     int type;           // 元素的類型,如是對象還是數組  
  6.   
  7.     char *valuestring;          // 如果是字符串  
  8.     int valueint;               // 如果是數值  
  9.     double valuedouble;         // 如果類型是cJSON_Number  
  10.   
  11.     char *string;               // “鍵”的名稱 
  12. } cJSON;  

json數據 
  1. {  
  2.     "name""Jack (\"Bee\") Nimble",   
  3.     "format": {  
  4.         "type":       "rect",   
  5.         "width":      1920,   
  6.         "height":     1080,   
  7.         "interlace":  false,   
  8.         "frame rate": 24  
  9.     }  


字符串解析成json結構體
  1. cJSON *root = cJSON_Parse(my_json_string);  

獲取某個元素 
  1. cJSON *format = cJSON_GetObjectItem(root,"format");  
  2. int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;  

json結構體轉換成字符串 
  1. char *rendered=cJSON_Print(root);  

刪除 
  1. cJSON_Delete(root);  

構建一個json結構體 
  1. cJSON *root,*fmt;  
  2. root=cJSON_CreateObject();    
  3. cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));  
  4. cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());  
  5. cJSON_AddStringToObject(fmt,"type",     "rect");  
  6. cJSON_AddNumberToObject(fmt,"width",        1920);  
  7. cJSON_AddNumberToObject(fmt,"height",       1080);  
  8. cJSON_AddFalseToObject (fmt,"interlace");  
  9. cJSON_AddNumberToObject(fmt,"frame rate",   24);  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章