cJSON使用
使用
2023-09-11 14:19:53 时间
c语言源码下载地址:https://github.com/DaveGamble/cJSON
其他语言源码下载地址:http://www.json.org/
用 cJSON_PrintUnformatted(root) 或者 cJSON_Print(root);来将json对象转换成普通的字符串,并且都是以该json对象的根为基点。两个API的区别即是:一个是没有格式的:也就是转换出的字符串中间不会有"\n" "\t"之类的东西存在,而cJSON_Print(root);打印出来是人看起来很舒服的格式。仅此而已。
cJSON的移植
JSON格式的生产
以下代码不需要执行cJSON_Delete(sub_js),因为删除父CJSON对象时,会删除子cJSON对象
cJSON *root=NULL; cJSON *sub_js=NULL; char *out=NULL; root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "ver", "v1.0.0"); cJSON_AddStringToObject(root, "did", "066EFF3638324D4D43183319"); cJSON_AddStringToObject(root, "hardware_ver", "V1.0.0"); cJSON_AddStringToObject(root, "software_ver", "V1.0.0"); cJSON_AddItemToObject(root, "data", sub_js = cJSON_CreateObject()); cJSON_AddNumberToObject(sub_js, "status", 1); cJSON_AddTrueToObject(sub_js, "material"); cJSON_AddTrueToObject(sub_js, "power_on"); cJSON_AddNumberToObject(sub_js, "qty", 123); cJSON_AddStringToObject(root, "dt", "2017-11-04T05:15:52"); out=cJSON_Print(root); cJSON_Delete(root); debug("%s\n",out); free(out);
打印出
{ "ver": "v1.0.0", "did": "066EFF3638324D4D43183319", "hardware_ver": "V1.0.0", "software_ver": "V1.0.0", "data": { "status": 1, "material": true, "power_on": true, "qty": 123 }, "dt": "2017-11-04T05:15:52" }
解析JSON格式(读取JSON格式数据,只要是在同一个{}内,不需要从上往下按顺序读,支持乱序)
{ "wifi": { "ssid": "hello World", "pwd": "234sdlfkj23" }, "mqtt": { "server": "192.168.31.240", "port": 1883, "clientid": "asdfghjklzxcvbnm", "username": "1234567asdfghj", "password": "asdfghjkerty" }, "option": { "working_signal_last_time": 15, "material_empty_signal_last_time": 6, "material_full_signal_last_time": 3, "status_send_time": 30 } }
UART_RxBuf_tmp就是上面的JSON格式数据
char *out; cJSON *json, *item1, *item2; json=cJSON_Parse((char const *)UART_RxBuf_tmp); if (!json) { debug("Error before:%s\n",cJSON_GetErrorPtr()); } else { item1 = cJSON_GetObjectItem(json, "wifi"); item2 = cJSON_GetObjectItem(item1, "ssid"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "pwd"); debug("%s: %s", item2->string, item2->valuestring); debug("%s",wifi_information); item1 = cJSON_GetObjectItem(json, "mqtt"); item2 = cJSON_GetObjectItem(item1, "server"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "port"); debug("%s: %d", item2->string, item2->valueint); debug("%s",server_information); item2 = cJSON_GetObjectItem(item1, "clientid"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "username"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "password"); debug("%s: %s", item2->string, item2->valuestring); item1 = cJSON_GetObjectItem(json, "option"); item2 = cJSON_GetObjectItem(item1, "working_signal_last_time"); debug("%s: %d", item2->string, item2->valueint); item2 = cJSON_GetObjectItem(item1, "material_empty_signal_last_time"); debug("%s: %d", item2->string, item2->valueint); item2 = cJSON_GetObjectItem(item1, "material_full_signal_last_time"); debug("%s: %d", item2->string, item2->valueint); item2 = cJSON_GetObjectItem(item1, "status_send_time"); debug("%s: %d", item2->string, item2->valueint); out=cJSON_Print(json); cJSON_Delete(json); debug("%s\n",out); free(out); }
创建带数组的JSON
cJSON *root=NULL; cJSON *sub_js=NULL; char *out=NULL; root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "ver", protocol_ver); cJSON_AddStringToObject(root, "did", (char const*)g_MCU_uniqueID_str); cJSON_AddStringToObject(root, "hardware_ver", hardware_ver); cJSON_AddStringToObject(root, "software_ver", software_ver); cJSON_AddItemToObject(root, "data", sub_js = cJSON_CreateObject()); cJSON *array; array = cJSON_CreateArray(); cJSON_AddItemToObject(sub_js, "M", array); cJSON_AddItemToArray(array, cJSON_CreateBool(1)); cJSON_AddItemToArray(array, cJSON_CreateBool(1)); cJSON_AddItemToArray(array, cJSON_CreateBool(1)); cJSON_AddItemToArray(array, cJSON_CreateBool(0)); cJSON_AddItemToArray(array, cJSON_CreateBool(0)); array = cJSON_CreateArray(); cJSON_AddItemToObject(sub_js, "D", array); cJSON_AddItemToArray(array, cJSON_CreateNumber(12312)); cJSON_AddItemToArray(array, cJSON_CreateNumber(12334)); cJSON_AddItemToArray(array, cJSON_CreateNumber(967425)); char time[20]; get_formatted_time(time); cJSON_AddStringToObject(root, "dt", (char const*)time); out=cJSON_Print(root); cJSON_Delete(root); debug("%s\n",out); MQTT_publish(out, "init"); vPortFree(out);
相关文章
- PySide6、PyQt6、ui文件转py文件、Qt Designer 使用
- 反调试 - r3 使用 NtQuerySystemInformation 获取 KdKdDebuggerEnable 和 KdDebuggerNotPresent
- memset()函数的使用
- 《HTML5 开发实例大全》——1.4 使用CSS修饰HTML 5页面
- 《iOS应用开发指南——使用HTML5、CSS3和JavaScript》——导读
- Android Design Support Library使用详解
- Sentinel安装使用和配置,sentinel-dashboard安装使用和配置
- 【gitee及git的简单使用教程】从零开始到下载、更改文件
- Kivy教程大全之 如何使用 Python 编写桌面和移动应用程序
- Vue中插槽(slot)的使用详解
- JDBC使用游标实现分页查询的方法
- 使用nodejs和express搭建http web服务
- 使用Docker部署Redis服务的方法
- 【第二届集成电路EDA备赛】一、Icarus Verilog(iverilog)快速上手使用(基于windows)
- linux下抓包学习--tcpdump的使用
- 不借助工具使用chrome浏览器直接长截图,