cJSON结构体构建
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
。。。
cJSON结构体构建的更多相关文章
- 『Python CoolBook』C扩展库_其四_结构体操作与Capsule
点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...
- gorm 结构体 预加载
结构体构建 type PlansApproval struct { ID uint Plans_Id int //plans编号 UpdateUser int //更新者 ...
- swift 的枚举、结构体、类
一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...
- Swift3.0P1 语法指南——类和结构体
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承
1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum Celebrity{ case DongXie,XiDu,Nandi,BeiGai } // 从左 ...
- swift学习笔记之-类和结构体
//类和结构体 import UIKit //类和结构体 /* 1.枚举enum.结构体struct和String.Array.Dictionary类型,都属于值传递类型,被赋值给新的常量或变量时传递 ...
- swift中的结构体和枚举
Swift 里的结构体非常特殊. 类是面向对象编程语言中传统的结构单元.和结构体相比,Swift 的类支持实现继承,(受限的)反射,析构函数和多所有者. 既然类比结构体强大这么多,为什么还要使用结构体 ...
- swift选择类或结构体
按照通用的准则,当符合一条或多条以下条件时,请考虑构建结构体: 结构体的主要目的是用来封装少量相关简单数据值. 有理由预计一个结构体实例在赋值或传递时,封装的数据将会被拷贝而不是被引用. ? 任何在结 ...
- 内核中用于数据接收的结构体struct msghdr(转)
内核中用于数据接收的结构体struct msghdr(转) 我们从一个实际的数据包发送的例子入手,来看看其发送的具体流程,以及过程中涉及到的相关数据结构.在我们的虚拟机上发送icmp回显请求包,pin ...
随机推荐
- C语言的补码表示和unsigned及signed的转换
这东西实际编程时一直无视的,范围小了就换个大点的表示形式,但是总觉得基础知识还是掌握得好,免得到时候用移位运算或类型转换或笔试题时要花时间想. C语言的基本类型有char.int.float.doub ...
- 修改http请求文件为本地文件的一种方法:hook InternetReadFile 和 HttpOpenRequest
今天没事的时候学了一下easyhook来hook本进程API,确实很简单就能hook.然后想到这个问题:替换webbrowser请求的文件为本地文件.有什么用就不说了,都懂.因为没有用API写过htt ...
- htm标签的语意
标签名 英文全拼 标签语意 div division 分割 span span 范围 ol ordered list 排序列表 ul unordered list 不排序列表 li list item ...
- jquery easy ui 的formatter 格式化函数代码
- Django-2的路由层(URLconf)
URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...
- vue引入jQuery、bootstrap
vue引入jQuery.bootstrap 1.使用vue-cli构建的vue项目 2.npm安装jquery.bootstrap npm install jquery 3.修改build/webpa ...
- MyEclipse显示 Install new software 在线安装插件选项
转自:https://blog.csdn.net/greatpresident/article/details/8950869 昨天不知道怎么就删除了电脑中的eclipse 我x,还原不回来了. 今天 ...
- TestNG Hello World入门示例
https://www.yiibai.com/testng/hello-world-example.html https://www.yiibai.com/testng/ 作为一个经典的入门例子,这里 ...
- rails 网站字体
方法1,在rubymine下查找所有css,scss,sass,less,修改所有带font-family的内容,删除public文件夹下面的缓存css,查看效果.如 body { backgroun ...
- sortingOrder,sortingLayer
sortingOrder 是sortingLayer内的分级 sortingLayer是抽象的分层,用于决定2D物体绘制的先后顺序. 2D物体分两类:sprite和UI. sprite虽是2D,却可以 ...