轉自:http://buluzhai.iteye.com/blog/845404 首先感謝作者!!
我使用的是cJSON:http://sourceforge.net/projects/cjson/
先看json的數據結構
c中沒有對象,所以json數據是采用鏈表存儲的
C代碼如下:
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; // The item's name string, if this item is the child of, or is in the list of subitems of an object. 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 } 10 }