cJsonFiles数据结构
生活随笔
收集整理的這篇文章主要介紹了
cJsonFiles数据结构
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先看json的數(shù)據(jù)結(jié)構(gòu)
c中沒(méi)有對(duì)象,所以json數(shù)據(jù)是采用鏈表存儲(chǔ)的
view sourceprint? 01.typedef struct cJSON { 02.struct cJSON *next,*prev;????// 數(shù)組 對(duì)象數(shù)據(jù)中用到 03.struct cJSON *child;????????// 數(shù)組 和對(duì)象中指向子數(shù)組對(duì)象或值 04.?? 05.int type;????????????// 元素的類型,如是對(duì)象還是數(shù)組 06.?? 07.char *valuestring;????????????// 如果是字符串 08.int valueint;????????????????// 如果是數(shù)值 09.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;比如你有一個(gè)json數(shù)據(jù)
view sourceprint? 01.{ 02."name":?"Jack (\"Bee\") Nimble", 03."format": { 04."type":???????"rect", 05."width":??????1920, 06."height":?????1080, 07."interlace":??false, 08."frame rate":?24 09.} 10.}那么你可以
1:講字符串解析成json結(jié)構(gòu)體。
cJSON?*root?=?cJSON_Parse(my_json_string);
2:獲取某個(gè)元素
cJSON?*format?=?cJSON_GetObjectItem(root,”format”);
int?framerate?=?cJSON_GetObjectItem(format,”frame?rate”)->valueint;
3:講json結(jié)構(gòu)體轉(zhuǎn)換成字符串
char?*rendered=cJSON_Print(root);
4:刪除
cJSON_Delete(root);
5:構(gòu)建一個(gè)json結(jié)構(gòu)體
view sourceprint? 01.cJSON *root,*fmt; 02.root=cJSON_CreateObject(); 03.cJSON_AddItemToObject(root,?"name", cJSON_CreateString("Jack (\"Bee\") Nimble")); 04.cJSON_AddItemToObject(root,?"format", fmt=cJSON_CreateObject()); 05.cJSON_AddStringToObject(fmt,"type",????????"rect"); 06.cJSON_AddNumberToObject(fmt,"width",????????1920); 07.cJSON_AddNumberToObject(fmt,"height",????????1080); 08.cJSON_AddFalseToObject (fmt,"interlace"); 09.cJSON_AddNumberToObject(fmt,"frame rate",????24);?
總結(jié)
以上是生活随笔為你收集整理的cJsonFiles数据结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2n个整数分为两组,使两组和差的绝对值最
- 下一篇: GCC参数