从天气预报API实战解析:手把手教你用cJSON处理嵌套数组与对象(避坑指南)
从天气预报API实战解析手把手教你用cJSON处理嵌套数组与对象避坑指南天气预报API返回的JSON数据往往结构复杂包含多层嵌套的对象和数组。对于C语言开发者来说使用轻量级的cJSON库解析这类数据时稍有不慎就会遇到空指针访问、内存泄漏等问题。本文将以一个真实的天气预报API响应为例带你逐步拆解嵌套结构的解析过程并分享几个关键避坑技巧。1. 天气预报API的数据结构分析典型的天气预报API响应包含当前天气数据和未来几天预报数组。以下是我们将要解析的JSON结构示例{ city: Beijing, update_time: 2023-05-20 14:00, forecast: [ { date: 2023-05-20, temp_max: 28, temp_min: 18, condition: sunny, humidity: 45 }, { date: 2023-05-21, temp_max: 26, temp_min: 17, condition: cloudy, humidity: 60 } ] }这种结构有三个显著特点多层嵌套最外层是城市信息forecast字段包含一个数组动态长度forecast数组长度不固定取决于预报天数混合类型包含字符串、数字等多种数据类型2. cJSON解析基础从简单对象开始在解析复杂结构前先确保掌握基本对象解析。cJSON提供了几个核心函数cJSON *root cJSON_Parse(json_string); // 解析JSON字符串 cJSON *item cJSON_GetObjectItem(root, key); // 获取对象字段 int value item-valueint; // 获取整数值 char *str item-valuestring; // 获取字符串值 cJSON_Delete(root); // 释放内存注意每次调用cJSON_Parse后必须对应调用cJSON_Delete否则会导致内存泄漏。3. 解析嵌套对象与数组的完整流程3.1 安全解析的基本模式解析任何JSON数据都应遵循以下安全模式cJSON *root cJSON_Parse(json_string); if (!root) { printf(Parse error: %s\n, cJSON_GetErrorPtr()); return; } // 解析逻辑... cJSON_Delete(root);3.2 逐层解析嵌套对象以获取城市信息为例cJSON *city_obj cJSON_GetObjectItem(root, city); if (city_obj city_obj-type cJSON_String) { printf(City: %s\n, city_obj-valuestring); } cJSON *time_obj cJSON_GetObjectItem(root, update_time); if (time_obj time_obj-type cJSON_String) { printf(Update time: %s\n, time_obj-valuestring); }3.3 遍历数组的正确方式处理forecast数组需要特别注意cJSON *forecast_array cJSON_GetObjectItem(root, forecast); if (forecast_array forecast_array-type cJSON_Array) { int array_size cJSON_GetArraySize(forecast_array); for (int i 0; i array_size; i) { cJSON *day_item cJSON_GetArrayItem(forecast_array, i); if (!day_item) continue; cJSON *date_item cJSON_GetObjectItem(day_item, date); cJSON *temp_max_item cJSON_GetObjectItem(day_item, temp_max); if (date_item date_item-type cJSON_String) { printf(Date: %s\n, date_item-valuestring); } if (temp_max_item temp_max_item-type cJSON_Number) { printf(Max temp: %d\n, temp_max_item-valueint); } } }4. 五大常见陷阱与解决方案4.1 空指针问题问题直接访问未检查的cJSON指针可能导致程序崩溃解决始终检查指针和类型// 错误示范 printf(%s\n, cJSON_GetObjectItem(root, nonexistent)-valuestring); // 正确做法 cJSON *item cJSON_GetObjectItem(root, nonexistent); if (item item-type cJSON_String) { printf(%s\n, item-valuestring); }4.2 内存泄漏问题cJSON_Print分配的内存需要手动释放解决为每个cJSON_Print调用配对freechar *json_str cJSON_Print(root); // 使用json_str... free(json_str); // 必须释放4.3 类型混淆问题错误判断字段类型导致数据错误解决严格检查type字段cJSON *item cJSON_GetObjectItem(root, temp); if (item) { if (item-type cJSON_Number) { int temp item-valueint; } else if (item-type cJSON_String) { char *temp_str item-valuestring; } }4.4 数组越界问题错误计算数组长度导致越界解决使用cJSON_GetArraySize获取准确长度int array_size cJSON_GetArraySize(array); for (int i 0; i array_size; i) { // 安全访问 }4.5 释放顺序错误问题先删除父节点导致子节点访问错误解决从内到外释放或直接删除根节点// 正确做法 cJSON_Delete(root); // 自动递归释放所有子节点 // 错误做法 cJSON_Delete(child); cJSON_Delete(parent); // 此时child已被释放导致未定义行为5. 模块化解析函数封装为提高代码复用性建议将解析逻辑封装成独立函数typedef struct { char date[20]; int temp_max; int temp_min; char condition[20]; } WeatherForecast; int parse_forecast(cJSON *root, WeatherForecast *forecasts, int max_items) { cJSON *array cJSON_GetObjectItem(root, forecast); if (!array || array-type ! cJSON_Array) return 0; int count 0; int array_size cJSON_GetArraySize(array); for (int i 0; i array_size count max_items; i) { cJSON *item cJSON_GetArrayItem(array, i); if (!item) continue; cJSON *date cJSON_GetObjectItem(item, date); cJSON *temp_max cJSON_GetObjectItem(item, temp_max); cJSON *temp_min cJSON_GetObjectItem(item, temp_min); cJSON *condition cJSON_GetObjectItem(item, condition); if (date date-type cJSON_String) { strncpy(forecasts[count].date, date-valuestring, sizeof(forecasts[count].date)-1); } if (temp_max temp_max-type cJSON_Number) { forecasts[count].temp_max temp_max-valueint; } // 其他字段处理... count; } return count; }使用示例WeatherForecast forecasts[7]; int num_forecasts parse_forecast(root, forecasts, 7); for (int i 0; i num_forecasts; i) { printf(%s: %d°C\n, forecasts[i].date, forecasts[i].temp_max); }6. 性能优化技巧处理大型JSON数据时可以考虑以下优化手段批量处理避免频繁的小内存分配预解析只解析需要的字段内存池为cJSON节点使用自定义内存管理// 示例使用内存池优化 void *pool_alloc(size_t size) { // 实现自定义内存分配 } void pool_free(void *ptr) { // 实现自定义内存释放 } // 设置cJSON内存钩子 cJSON_Hooks hooks {pool_alloc, pool_free}; cJSON_InitHooks(hooks);在实际项目中我发现最容易被忽视的是cJSON_Print生成字符串的释放问题。曾经因为忘记free这些字符串导致服务运行几天后内存耗尽崩溃。现在我会在代码审查时特别关注每个cJSON_Print是否有对应的free调用。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2577682.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!