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 ...
随机推荐
- Windows下安装HBase
本文转载自:http://blog.csdn.net/kangkanglou/article/details/30748139 本文主要参照Hbase官网:http://hbase.apache.or ...
- 使用Java读取配置文件
实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOExcep ...
- JeeSite导出多条数据(加复选框)demo
表格图: jsp: 后台: @RequiresPermissions("shwindow:advertisementPutInList:view") @RequestMapping ...
- nginx转发请求
location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_add ...
- VRF实例说明
Virtual Routing Forwarding VPN路由转发表,也称VPN-instance(VPN实例),是PE为直接相连的site建立并维护的一个专门实体,每个site在PE上 ...
- python中nltk的下载安装方式
首先去http://nltk.org/install.html下载相关的安装程序,然后 在cmd窗口中,进入到python的文件夹内的 Scripts内,运行easy_install pip 安装Py ...
- opennebula onenebula
http://www.eucalyptus.com/blog/2013/01/07/opennebula-38-%E2%80%94-%E7%9B%91%E6%8E%A7 [云监控] http://ww ...
- C#条形码生成(五)----Web下的测试
Html部分 <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server" ...
- 「小程序JAVA实战」小程序头像图片上传(下)(45)
转自:https://idig8.com/2018/09/09/xiaochengxujavashizhanxiaochengxutouxiangtupianshangchuan44/ 接下来,我们应 ...
- mysql彻底删除
yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...