百度天气api


title: 百度天气api
date: 2021-04-11 22:21:11
tags: api

如果调用百度天气官网给的api是会出现错误的

http://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=你的ak  //GET请求

会提示mcode错误,我们需要添加一些参数

http://api.map.baidu.com/weather/v1/?district_id=行政区划编码&data_type=all&mcode=你的安全码&output=json&ak=你的ak

例如:

http://api.map.baidu.com/weather/v1/?district_id=511100&data_type=all&mcode=CE:F7:45:62:AE:78:28:68:A0:A5:6D:70:CD:DF:5B:7C:C5:B9:91:CF;com.mahao.leshan&output=json&ak=SrYlotSQCWdHHulPveAULFzFF9BjXE1C

如上,我添加了mcode和output,安全码在百度控制台中申请项目后可以进行查看,值为SHA1的值+分号+包名

接下来就有了json数据,如下为511100也就是乐山的天气数据

{

  • status: 0,

  • result:

    {

    • location:

      {

      • country: “中国”,
      • province: “四川省”,
      • city: “乐山市”,
      • name: “乐山”,
      • id: “511100”,

      },

    • now:

      {

      • text: “多云”,
      • temp: 15,
      • feels_like: 19,
      • rh: 100,
      • wind_class: “1级”,
      • wind_dir: “西风”,
      • uptime: “20210411213000”,

      },

    • forecasts:

      [

      • {

        • text_day: “小雨”,
        • text_night: “小雨”,
        • high: 17,
        • low: 13,
        • wc_day: “<3级”,
        • wd_day: “静风”,
        • wc_night: “<3级”,
        • wd_night: “静风”,
        • date: “2021-04-11”,
        • week: “星期日”,

        },

      • {

        • text_day: “多云”,
        • text_night: “多云”,
        • high: 21,
        • low: 14,
        • wc_day: “<3级”,
        • wd_day: “静风”,
        • wc_night: “<3级”,
        • wd_night: “静风”,
        • date: “2021-04-12”,
        • week: “星期一”,

        },

      • {

        • text_day: “阴”,
        • text_night: “小雨”,
        • high: 21,
        • low: 15,
        • wc_day: “<3级”,
        • wd_day: “静风”,
        • wc_night: “<3级”,
        • wd_night: “静风”,
        • date: “2021-04-13”,
        • week: “星期二”,

        },

      • {

        • text_day: “小雨”,
        • text_night: “小雨”,
        • high: 18,
        • low: 14,
        • wc_day: “<3级”,
        • wd_day: “静风”,
        • wc_night: “<3级”,
        • wd_night: “静风”,
        • date: “2021-04-14”,
        • week: “星期三”,

        },

      • {

        • text_day: “阴”,
        • text_night: “小雨”,
        • high: 19,
        • low: 14,
        • wc_day: “<3级”,
        • wd_day: “静风”,
        • wc_night: “<3级”,
        • wd_night: “静风”,
        • date: “2021-04-15”,
        • week: “星期四”,

        },

      ],

    },

  • message: “success”,

}

你可能感兴趣的:(json,百度)