cJsonFiles数据结构

先看json的数据结构

c中没有对象,所以json数据是采用链表存储的

01. typedef struct cJSON {
02. struct cJSON *next,*prev;<wbr><wbr><wbr>// 数组 对象数据中用到</wbr></wbr></wbr>
03. struct cJSON *child;<wbr><wbr><wbr><wbr><wbr><wbr>// 数组 和对象中指向子数组对象或值</wbr></wbr></wbr></wbr></wbr></wbr>
04. <wbr></wbr><wbr></wbr>
05. int type;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 元素的类型,如是对象还是数组</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
06. <wbr></wbr><wbr></wbr>
07. char *valuestring;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 如果是字符串</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
08. int valueint;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 如果是数值</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
09. doublevaluedouble;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 如果类型是cJSON_Number</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
10. <wbr></wbr><wbr></wbr>
11. char *string;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// The item's name string, if this item is the child of, or is in the list of subitems of an object.</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
12. } cJSON;

比如你有一个json数据

01. {
02. "name":"Jack (\"Bee\") Nimble",
03. "format": {
04. "type":<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>"rect",
05. "width":<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>1920,
06. "height":<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>1080,
07. "interlace":<wbr>false,</wbr>
08. "frame rate":24
09. }
10. }

那么你可以

1:讲字符串解析成json结构体。

cJSON<wbr>*root<wbr>=<wbr>cJSON_Parse(my_json_string);<br> 2:获取某个元素</wbr></wbr></wbr>

cJSON<wbr>*format<wbr>=<wbr>cJSON_GetObjectItem(root,”format”);</wbr></wbr></wbr>

int<wbr>framerate<wbr>=<wbr>cJSON_GetObjectItem(format,”frame<wbr>rate”)-&gt;valueint;</wbr></wbr></wbr></wbr>

3:讲json结构体转换成字符串

char<wbr>*rendered=cJSON_Print(root);</wbr>

4:删除

cJSON_Delete(root);

5:构建一个json结构体

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",<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>"rect");
06. cJSON_AddNumberToObject(fmt,"width",<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>1920);
07. cJSON_AddNumberToObject(fmt,"height",<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>1080);
08. cJSON_AddFalseToObject (fmt,"interlace");
09. cJSON_AddNumberToObject(fmt,"frame rate",<wbr><wbr><wbr></wbr></wbr></wbr>24);
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值