cJson使用方法
发布时间:2020-12-16 19:50:24 所属栏目:百科 来源:网络整理
导读:我使用的是cJSON: http://sourceforge.net/projects/cjson/先看json的数据结构c中没有对象,所以json数据是采用链表存储的 typedef struct cJSON {struct cJSON *next,*prev;// 数组 对象数据中用到struct cJSON *child;// 数组 和对象中指向子数组对象或值i
我使用的是cJSON:
http://sourceforge.net/projects/cjson/先看json的数据结构c中没有对象,所以json数据是采用链表存储的
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数据 { "name": "Jack ("Bee") Nimble","format": { "type": "rect","width": 1920,"height": 1080,"interlace": false,"frame rate": 24 } } 那么你可以 1:讲字符串解析成json结构体。 cJSON *root = cJSON_Parse(my_json_string); 2:获取某个元素 cJSON *format = cJSON_GetObjectItem(root,"format"); int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; 3:讲json结构体转换成字符串 char *rendered=cJSON_Print(root); 4:删除 cJSON_Delete(root); 5:构建一个json结构体 cJSON *root,*fmt; root=cJSON_CreateObject(); cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack ("Bee") Nimble")); cJSON_AddItemToObject(root,"format",fmt=cJSON_CreateObject()); cJSON_AddStringToObject(fmt,"type","rect"); cJSON_AddNumberToObject(fmt,"width",1920); cJSON_AddNumberToObject(fmt,"height",1080); cJSON_AddFalseToObject (fmt,"interlace"); cJSON_AddNumberToObject(fmt,"frame rate",24); 原文地址: http://buluzhai.iteye.com/blog/845404 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |