应届生招聘去哪个网站,广州十大跨境电商公司排名,开发一个物流app需要多少钱,品牌网站品牌理念老旧的后果一、概述
当前文章介绍如何在Linux#xff08;Ubuntu#xff09;下使用C语言调用libcurl库获取天气预报的方法。通过HTTP GET请求访问百度天气API#xff0c;并解析返回的JSON数据#xff0c;可以获取指定城市未来7天的天气预报信息。 二、设计思路
【1】使用libcurl库进…一、概述
当前文章介绍如何在LinuxUbuntu下使用C语言调用libcurl库获取天气预报的方法。通过HTTP GET请求访问百度天气API并解析返回的JSON数据可以获取指定城市未来7天的天气预报信息。 二、设计思路
【1】使用libcurl库进行HTTP GET请求
在代码中包含curl/curl.h头文件以便使用libcurl库使用curl_easy_init()函数初始化curl设置请求选项包括URL、写回调函数和写数据参数使用curl_easy_perform()函数执行请求
【2】编写回调函数将响应数据存储在内存中
定义一个结构体包含存储响应数据的指针和长度在回调函数中将响应数据拷贝到内存中并动态调整内存大小返回已拷贝的数据大小
【3】解析JSON数据
使用json_tokener_parse()函数解析返回的JSON数据使用json_object_object_get_ex()函数获取指定字段的值使用json_object_array_length()函数获取数组长度使用json_object_array_get_idx()函数获取数组中的元素使用json_object_get_string()函数获取字符串值
【4】打印天气预报信息
遍历获取到的天气预报数据依次获取日期、天气和温度使用printf()函数打印每一天的天气预报信息
三、关键代码
以下是主要的代码片段
// 定义回调函数用于将响应数据存储在内存中
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream) {// ...
}// 子函数用于获取指定城市未来7天的天气预报
int get_weather_forecast(const char *city) {// ...
}int main() {const char *city your_city_code;int ret get_weather_forecast(city);// ...
}四、使用说明
【1】替换API密钥和城市代码在示例代码中将your_ak和your_city_code替换为你自己的百度API密钥和城市代码。
【2】编译代码使用合适的C编译器如gcc编译代码。
gcc -o download_program download_program.c -lcurl【3】运行代码在终端中运行生成的可执行文件。
./download_program【4】查看天气预报程序会打印出指定城市未来7天的天气预报信息。
五、完整代码
HTTP GET请求访问百度天气API并解析返回的JSON数据获取需要的天气信息。
#include stdio.h
#include stdlib.h
#include string.h
#include curl/curl.h
#include json-c/json.h// 定义回调函数用于将响应数据存储在内存中
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream) {size_t realsize size * nmemb;struct string *mem (struct string *)stream;mem-ptr realloc(mem-ptr, mem-len realsize 1);if (mem-ptr NULL) {fprintf(stderr, 内存分配失败\n);return 0;}memcpy((mem-ptr[mem-len]), ptr, realsize);mem-len realsize;mem-ptr[mem-len] \0;return realsize;
}// 子函数用于获取指定城市未来7天的天气预报
int get_weather_forecast(const char *city) {char url[256];sprintf(url, https://api.map.baidu.com/weather/v1/?district_id%sakyour_ak, city);CURL *curl curl_easy_init();struct string response;response.ptr malloc(1);response.len 0;if (curl response.ptr) {// 设置请求选项curl_easy_setopt(curl, CURLOPT_URL, url);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);// 执行请求CURLcode res curl_easy_perform(curl);if (res ! CURLE_OK) {fprintf(stderr, 请求失败: %s\n, curl_easy_strerror(res));free(response.ptr);curl_easy_cleanup(curl);return -1;}// 解析JSON数据struct json_object *json json_tokener_parse(response.ptr);if (json NULL) {fprintf(stderr, JSON解析失败\n);free(response.ptr);curl_easy_cleanup(curl);return -1;}// 解析天气预报struct json_object *result, *weather_data;json_object_object_get_ex(json, result, result);json_object_object_get_ex(result, weather_data, weather_data);int i;int num_days json_object_array_length(weather_data);for (i 0; i num_days; i) {struct json_object *day json_object_array_get_idx(weather_data, i);const char *date, *weather, *temperature;date json_object_get_string(json_object_object_get(day, date));weather json_object_get_string(json_object_object_get(day, weather));temperature json_object_get_string(json_object_object_get(day, temperature));printf(日期%s\n天气%s\n温度%s\n\n, date, weather, temperature);}free(response.ptr);json_object_put(json);} else {fprintf(stderr, 初始化失败\n);if (response.ptr) {free(response.ptr);}if (curl) {curl_easy_cleanup(curl);}return -1;}curl_easy_cleanup(curl);return 0;
}int main() {const char *city your_city_code;int ret get_weather_forecast(city);if (ret 0) {printf(天气预报获取成功\n);} else {printf(天气预报获取失败\n);}return 0;
}在示例代码中使用curl_easy_setopt函数设置HTTP GET请求的URL并通过CURLOPT_WRITEFUNCTION和CURLOPT_WRITEDATA选项指定回调函数将响应数据存储在内存中。
然后使用json_tokener_parse函数解析返回的JSON数据并提取其中的天气预报信息。通过json_object_object_get和json_object_array_get_idx等函数获取JSON对象和数组中的数据。
注意代码中的URL中的YOUR_AK和YOUR_CITY_CODE需要使用你自己的百度API密钥和城市代码替换。
通过调用get_weather_forecast函数可以获取指定城市未来7天的天气预报并打印出来。 文章转载自: http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.dwztj.cn.gov.cn.dwztj.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.rbnp.cn.gov.cn.rbnp.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.kphyl.cn.gov.cn.kphyl.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.dcccl.cn.gov.cn.dcccl.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.ssgqc.cn.gov.cn.ssgqc.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.jlrym.cn.gov.cn.jlrym.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.rzjfn.cn.gov.cn.rzjfn.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.kynf.cn.gov.cn.kynf.cn