cJSON学习笔记 续集
转自:http://blog.csdn.net/xukai871105/article/details/33013455
- void create_single_number(void) {
- cJSON *root;
- char *out;
- int value = 24;
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddItemToObject(root, "value", cJSON_CreateNumber(value));
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "value": 24
- }
- #endif
- }
- void create_single_string(void) {
- cJSON *root;
- char *out;
- char *name = "xukai871105";
- root = cJSON_CreateObject(); // 创建根
- // 方法 使用cJSON_AddItemToObject,推荐使用
- cJSON_AddItemToObject(root, "name", cJSON_CreateString(name));
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "name": "xukai871105"
- }
- #endif
- }
- void create_bool(void) {
- cJSON *root;
- char *out;
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddItemToObject(root, "success", cJSON_CreateFalse());
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "success": false
- }
- #endif
- }
- void create_simple(void) {
- cJSON *root;
- char *out;
- int list[4] = {5,6,7,8};
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddItemToObject(root, "lists", cJSON_CreateIntArray(list, 4));
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "lists": [5, 6, 7, 8]
- }
- #endif
- }
- void create_complex(void) {
- cJSON *root, *rows, *row;
- char *out;
- int i = 0;
- char *title[3] = { "树莓派学习笔记——索引博文",
- "树莓派学习笔记——GPIO功能学习",
- "物联网学习笔记——索引博文"};
- char *url[3] = { "http://blog.csdn.net/xukai871105/article/details/23115627",
- "http://blog.csdn.net/xukai871105/article/details/12684617",
- "http://blog.csdn.net/xukai871105/article/details/23366187"};
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddNumberToObject(root, "total", 3);
- // 在object中加入array
- cJSON_AddItemToObject(root, "rows", rows = cJSON_CreateArray());
- for(i = 0; i < 3; i++) {
- // 在array中加入object
- cJSON_AddItemToArray(rows, row = cJSON_CreateObject());
- cJSON_AddItemToObject(row, "title", cJSON_CreateString(title[i]));
- cJSON_AddItemToObject(row, "url", cJSON_CreateString(url[i]));
- }
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "total": 3,
- "rows": [{
- "title": "树莓派学习笔记——索引博文",
- "url": "http://blog.csdn.net/xukai871105/article/details/23115627"
- }, {
- "title": "树莓派学习笔记——GPIO功能学习",
- "url": "http://blog.csdn.net/xukai871105/article/details/12684617"
- }, {
- "title": "物联网学习笔记——索引博文",
- "url": "http://blog.csdn.net/xukai871105/article/details/23366187"
- }]
- }
- #endif
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。