c语言利用天气api,天气预报API_01

天气预报API

聚合API

注册个人账号,实名认证后每天有100次免费请求额度。免费API中包括天气预报、新闻头条、笑话大全、万年历等。

3ab94710dd6e

聚合API

天气预报API

返回格式:json

请求方式:http get/post

接口备注:通过城市名称或城市ID查询天气预报情况

请求参数说明:

名称

必填

类型

说明

city

string

要查询的城市名称/id,城市名称如:温州、上海、北京,需要utf8 urlencode

key

string

在个人中心->我的数据,接口名称上方查看

返回参数说明:

名称

类型

说明

error_code

int

返回码,0为查询成功

reason

string

返回说明

result

string

返回结果集

-

-

-

realtime

-

当前天气详情情况

info

string

天气情况,如:晴、多云

wid

string

天气标识id,可参考小接口2

temperature

string

温度,可能为空

humidity

string

湿度,可能为空

direct

string

风向,可能为空

power

string

风力,可能为空

aqi

string

空气质量指数,可能为空

-

-

-

future

-

近5天天气情况

date

string

日期

temperature

string

温度,最低温/最高温

weather

string

天气情况

direct

string

风向

JSON返回示例:

{

"reason": "查询成功",

"result": {

"city": "苏州",

"realtime": {

"temperature": "4",

"humidity": "82",

"info": "阴",

"wid": "02",

"direct": "西北风",

"power": "3级",

"aqi": "80"

},

"future": [

{

"date": "2019-02-22",

"temperature": "1/7℃",

"weather": "小雨转多云",

"wid": {

"day": "07",

"night": "01"

},

"direct": "北风转西北风"

},

{

"date": "2019-02-23",

"temperature": "2/11℃",

"weather": "多云转阴",

"wid": {

"day": "01",

"night": "02"

},

"direct": "北风转东北风"

},

{

"date": "2019-02-24",

"temperature": "6/12℃",

"weather": "多云",

"wid": {

"day": "01",

"night": "01"

},

"direct": "东北风转北风"

},

{

"date": "2019-02-25",

"temperature": "5/12℃",

"weather": "小雨转多云",

"wid": {

"day": "07",

"night": "01"

},

"direct": "东北风"

},

{

"date": "2019-02-26",

"temperature": "5/11℃",

"weather": "多云转小雨",

"wid": {

"day": "01",

"night": "07"

},

"direct": "东北风"

}

]

},

"error_code": 0

}

Linux下通过C获取天气

通过curl命令进行http get请求,并使用cJSON库解析返回结果

weather.c

#include

#include

#include "include.h"

//!!!使用时请换成自己的KEY

#define API_KEY "自己的KEY"

//输出菜单

int menu();

//请求对应城市的天气

int simpleWeather(int cityIndex);

enum{

BEJING = 0,

SHANGHAI = 1,

FUZHOU = 2,

SHENZHENG = 3,

CITY_COUNT,

};

char *city[CITY_COUNT] = {

"北京",

"上海",

"福州",

"深圳"

};

int main(int argc, char const *argv[])

{

int input = 0;

while (1)

{

menu();

scanf("%d", &input);

switch (input)

{

case 1:

//printf("北京\n");

simpleWeather(BEJING);

break;

case 2:

//printf("上海\n");

simpleWeather(SHANGHAI);

break;

case 3:

//printf("福州\n");

simpleWeather(FUZHOU);

break;

default:

printf("未知菜单,程序退出\n");

return 0;

}

}

return 0;

}

int menu()

{

printf("天气预报\n");

printf("1.北京\n");

printf("2.上海\n");

printf("3.福州\n");

printf("输入序号获取天气:\n");

return 0;

}

int simpleWeather(int cityIndex)

{

if(cityIndex >= CITY_COUNT){

printf("查询不到城市数据!\n");

return 0;

}

printf("查询城市 :%s\n", city[cityIndex]);

char cmd[MAX_LEN] = {0};

char result[RESULT_BUF_LEN] = {0};

//生成API请求url

sprintf(cmd, "curl -s \"http://apis.juhe.cn/simpleWeather/query?city=%s&key=%s\"", city[cityIndex], API_KEY);

popenCmd(cmd, result);

//printf("result :%s\n", result);

cJSON * root = NULL;

cJSON * api_result = NULL, *api_realtime = NULL;

root = cJSON_Parse(result);

if(NULL == root){

printf("解析json数据失败!\n");

return -1;

}

//先查找result

api_result = cJSON_GetObjectItem(root, "result");

//再查找realtime

api_realtime = cJSON_GetObjectItem(api_result, "realtime");

//printf("%s\n", cJSON_Print(api_realtime));

/*

"realtime": {

"temperature": "30",

"humidity": "48",

"info": "阴",

"wid": "02",

"direct": "南风",

"power": "2级",

"aqi": "65"

}

*/

cJSON *temp = cJSON_GetObjectItem(api_realtime, "temperature");

cJSON *humidity = cJSON_GetObjectItem(api_realtime, "humidity");

cJSON *info = cJSON_GetObjectItem(api_realtime, "info");

cJSON *direct = cJSON_GetObjectItem(api_realtime, "direct");

printf("温度:%s度\n", temp->valuestring);

printf("湿度:%s度\n", humidity->valuestring);

printf("天气:%s\n", info->valuestring);

printf("风向:%s\n", direct->valuestring);

return 0;

}

tools.c

目前读取popen结果使用最大缓存102400,最好修改代码将缓存改小,重复读取。

#include

#include "include.h"

int popenCmd(char *cmd, char *result)

{

//运行命令,将结果保存到results

FILE *fp = popen(cmd, "r");

if (NULL == fp)

{

printf("打开 file命令失败\n");

goto err;

}

//将结果读取到results字符串数组中

fgets(result, RESULT_BUF_LEN, fp);

if (fp)

{

pclose(fp);

fp = NULL;

}

return 0;

err:

if (fp)

{

pclose(fp);

fp = NULL;

}

return -1;

}

include.h

#include "cJSON.h"

#define MAX_LEN 256

#define RESULT_BUF_LEN 102400

//执行命令,将命令结果保存到result

int popenCmd(char *cmd, char *result);

#编译命令:

gcc weather.c cJSON.c tools.c -lm -o weather

./weather

#输入结果:

天气预报

1.北京

2.上海

3.福州

输入序号获取天气:

1

查询城市 :北京

温度:25度

湿度:82度

天气:多云

风向:东北风

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值