JSON簡介:
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式,可以把JSON的結構理解成無序的、可嵌套(指value可以再嵌套一層key-value)的key-value鍵值對集合,這些key-value鍵值對是以結構體或數組的形式來組織的。同一級的key-value鍵值對之間是用一個“,”(逗號)隔開,每個key-value鍵值對是由一個key後面緊接一個“:”(冒號),冒號後面是這個key對應的value。Key是一個word,由大小寫字母、下劃線及數字組成,可以由雙引號封閉,也可以不加雙引號;而value的取值集爲:Number、Boolean(true或false)、null、String、Object及Array,如圖一:
(圖一)
1、Number:數值,包括整形數與浮點數,如:123、0.83、-2.7e10。其結構如圖二:
(圖二)
2、String:字符串,是以雙引號封閉起來的一串字符,使用反斜槓來轉義,如:\\、\n等,JSON中字符串的概念與C/C++或者JAVA語言裏的字符串概念差不多,如:”abc”。其結構如圖三:
(圖三)
3、Object:對象,也可理解成一個結構體,是以一對大括號封閉起來的無序的key-value鍵值對集合,例如:{name:"Susan", age:27, birthday:{year:1984, month:2, day:11}};也可以寫成:{"name":"Susan", "age":27, "birthday":{"year":1984, "month":2, "day":11}};其結構如圖四:
(圖四)
4、Array:數組,JSON的數組是一個以中括號封閉起來的value的集合,即數組內的各個成員的數據類型可以不一樣,這一點就跟C/JAVA的數組概念不同了。每個value之間是由一個“,”(逗號)隔開,例如:[123, abc, false, {name:mj}];其結構如圖五:
(圖五)
cJSON:C語言的JSON解釋器
在CJSON中,一個key-value鍵值對被解析並存放在一個cJSON結構體變量中,其value取值集爲:FALSE,TRUE,NULL,NUMBER,STRING,OBJECT,ARRAY。
- #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
cJSON 結構體:
- typedef struct cJSON
- {
- struct cJSON *next,*prev; //指向上一項/下一項
- struct cJSON *child; //指向下一級,也就是當type爲cJSON_Object或cJSON_Array時,此指針不爲空。
- int type;
- char *valuestring; // 當type爲cJSON_String時
- int valueint; // 當 type爲cJSON_Number時
- double valuedouble; //當type爲cJSON_Number時
- char *string; // 當前項的名稱,也就是key-value鍵值對的key
- } cJSON;
typedef struct cJSON {
14 struct cJSON *next,*prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/ GetObjectItem
15 struct cJSON *child; // An array or object item will have a child pointer pointing to a chain of the items in the arra y/object. int type; // The type of the item, as above.
18
19 char *valuestring; // The item's string, if type==cJSON_String
20 int valueint; // The item's number, if type==cJSON_Number
21 double valuedouble; // The item's number, if type==cJSON_Number
22
23 char *string; // The item's name string (=key), if this item is the child of, or is in the list of subitems of an object.
24 } cJSON; //並不存儲Array/Object的名字
在解析JSON過程中,從JSON格式描述的value數據到CJSON對象中存放的變量的一個映射關係如圖七:
(圖七)
對JSON格式的解析是使用cJSON_Parse()方法,其傳入的參數是一個JSON的Object/Array結構的字串,解析成功則返回一個cJSON結構體變量的指針,在使用完成後需要調用cJSON_Delete()將該指針銷燬。CJSON是以樹狀結構來組織內部的各個cJSON結構體變量的,一般地,要使用某個cJSON結構體變量,需要調用cJSON_GetObjectItem()方法並根據其父節點的cJSON結構體變量指針與該項的名稱來獲取其指針,舉個例子:
- bool bResult;
- char jsonString[] = “{result:true}”;
- //獲取result的值true
- cJSON* pItem = NULL;
- cJSON* pRoot = cJSON_Parse ( jsonString );
- if ( pRoot )
- {
- pItem = cJSON_GetObjectItem ( pRoot, “result” );
- if ( pItem )
- {
- bResult = pItem->valueint; //由於result的值type爲cJSON_False或cJSON_True,所以其值被存放在valueint變量中
- }
- cJSON_Delete ( pRoot );
- }
在上例中,不管result的值type爲何類型,都是通過調用cJSON_GetObjectItem()方法獲取其對應的cJSON結構體變量的指針,只是在處理其對應的值時會有所不同。如果result的值type爲cJSON_Object,則需要通過調用cJSON_GetObjectItem( pItem, “subItemKey”)來獲取其子Item的指針。在處理值type爲cJSON_Array的Item時,就需要再用到另外兩個API:cJSON_GetArraySize ()和cJSON_GetArrayItem()。我們舉個獲取一個數組成員值的例子:
- char* out;
- char jsonString[] = “{colors:[\“red\”, \“green\”,\ “blue\”, \“yellow\”, \“white\”]}”;
- cJSON* pArray = NULL;
- cJSON* pRoot = cJSON_Parse ( jsonString );
- if ( pRoot )
- {
- pArray = cJSON_GetObjectItem ( pRoot, “colors” );
- if ( pArray )
- {
- cJSON* pArrayItem = NULL;
- int nCount = cJSON_GetArraySize ( pArray ); //獲取pArray數組的大小
- for( int i = 0; i < nCount; i++)
- {
- pArrayItem = cJSON_GetArrayItem(pArray, i);
- out = cJSON_Print( pArrayItem ); //將pArrayItem的值以字串的形式打印到char型buffer上,cJSON_Print()會自動分配內存空間,用完需要釋放內存。
- SS_printf( “array item %d: %s\n”, i, out);
- Free( out );
- }
- }
- cJSON_Delete ( pRoot );
- }
在提取一個複雜的JSON格式的數據時,也只是將以上兩個例子使用到的方法進行組合調用罷了。所以對CJSON提供的API的使用是很簡單有效的。有了以上知識的瞭解,我們就可以編寫一些代碼將例一中的JSON解析並提取其中的數據。