首页 技术 正文
技术 2022年11月10日
0 收藏 671 点赞 3,308 浏览 3572 个字

cJSON结构体构建

一:cJSON的构建。

 int create_objects()
{
cJSON *root, *fmt, *img, *thm, *fld;
char *out;
int i; /* The index number. */
int ret = ; /* Here we construct several JSON objects. */ // ------------------构建第1个----------------------
/* The "Video" data type: */
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", );
cJSON_AddNumberToObject(fmt, "height", );
cJSON_AddFalseToObject (fmt, "interlace");
cJSON_AddNumberToObject(fmt, "frame rate", ); out = cJSON_Print(root); /* Print to text */
cJSON_Delete(root); /* Delete the cJSON object */
LOG_I(cjson_example, "%s\n", out); /* Print out the text */
cJSON_free(out); /* Release the string. */ // ------------------构建第2个----------------------
/* The "days of the week" array: */
const char *strings[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
root = cJSON_CreateStringArray(strings, ); out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out); // ------------------构建第3个----------------------
/* The matrix: */
int numbers[][] = {{, -, }, {, , }, {, , }};
root = cJSON_CreateArray();
for (i = ; i < ; i++) {
cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], ));
} /* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out); // ------------------构建第4个----------------------
/* The "gallery" item: */
int ids[] = {, , , };
root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "Image", img = cJSON_CreateObject());
cJSON_AddNumberToObject(img, "Width", );
cJSON_AddNumberToObject(img, "Height", );
cJSON_AddStringToObject(img, "Title", "View from 15th Floor");
cJSON_AddItemToObject(img, "Thumbnail", thm = cJSON_CreateObject());
cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
cJSON_AddNumberToObject(thm, "Height", );
cJSON_AddStringToObject(thm, "Width", "");
cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, )); out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out); // ------------------构建第5个----------------------
/* The array of "records": */
struct record fields[] = {
{"zip", 37.7668, -1.223959e+2, "", "SAN FRANCISCO", "CA", "", "US"},
{"zip", 37.371991, -1.22026e+2, "", "SUNNYVALE", "CA", "", "US"}
}; root = cJSON_CreateArray();
for (i = ; i < ; i++) {
cJSON_AddItemToArray(root, fld = cJSON_CreateObject());
cJSON_AddStringToObject(fld, "precision", fields[i].precision);
cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
cJSON_AddStringToObject(fld, "Address", fields[i].address);
cJSON_AddStringToObject(fld, "City", fields[i].city);
cJSON_AddStringToObject(fld, "State", fields[i].state);
cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
cJSON_AddStringToObject(fld, "Country", fields[i].country);
} /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out);
return ret;
}

二:打印如下

 {     "name":    "Jack (\"Bee\") Nimble",     "format":    {         "type":    "rect",         "width":    ,         "height":    ,         "interlace":    false,         "frame rate":         } } ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] [[, -, ], [, , ], [, , ]] {     "Image":    {         "Width":    ,         "Height":    ,         "Title":    "View from 15th Floor",         "Thumbnail":    {             "Url":    "http:/*www.example.com/image/481989943",             "Height":    ,             "Width":    ""         },         "IDs":    [, , , ]     } } [{         "precision":    "zip",         "Latitude":    37.7668,         "Longitude":    -122.3958999999999,         "Address":    "",         "City":    "SAN FRANCISCO",         "State":    "CA",         "Zip":    "",         "Country":    "US"     }, {         "precision":    "zip",         "Latitude":    37.371991,         "Longitude":    -122.02
。。。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298