cJSON使用
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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
2016-06-30 Altium Designer 15 --- PCB 3D View