c語言解析json資料__mysql

來源:互聯網
上載者:User
我使用的是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;  

比如你有一個json資料 
Javascript代碼   {       "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);  
2:擷取某個元素 
C代碼   cJSON *format = cJSON_GetObjectItem(root,"format");   int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;  
3:講json結構體轉換成字串 
C代碼   char *rendered=cJSON_Print(root);  
4:刪除 
C代碼   cJSON_Delete(root);  
5:構建一個json結構體 
C代碼  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.