本帖最后由 shory 于 2021-3-9 10:43 编辑
今天是3月9日,获取的3天天气预报信息:
- Blinker.attachWeatherForecast(weatherForecastData);
复制代码 返回的是从2月28日开始的3天:
- {"city":"襄阳市","province":"湖北","<font color="#ff0000">updateTime":"2021-02-28 10:23:50</font>","forecasts":[{"date":"<font color="#ff0000">2021-02-28</font>","week":"7","dayweather":"小雨","nightweather":"小雨","daytemp":"7","nighttemp":"5","daywind":"东北","nightwind":"东北","daypower":"≤3","nightpower":"≤3"},{"date":"<font color="#ff0000">2021-03-01</font>","week":"1","dayweather":"小雨","nightweather":"晴","daytemp":"8","nighttemp":"2","daywind":"北","nightwind":"北","daypower":"≤3","nightpower":"≤3"},{"date":"2021-03-02","week":"2","dayweather":"多云","nightweather":"多云","daytemp":"14","nighttemp":"3","daywind":"东","nightwind":"东","daypower":"≤3","nightpower":"≤3"},{"date":"<font color="#ff0000">2021-03-03</font>","week":"3","dayweather":"多云","nightweather":"多云","daytemp":"17","nighttemp":"7","daywind":"南","nightwind":"南","daypower":"≤3","nightpower":"≤3"}]}
复制代码 代码:
void weatherForecastData(const String & data)
{
const char* dayWeather;
const char* nightWeather;
const char* tempHigh;
const char* tempLow;
// BLINKER_LOG("weather: ", data);
DynamicJsonDocument jsonBuffer(1536);
DeserializationError error = deserializeJson(jsonBuffer, data);
if (error) {
BLINKER_LOG(F("deserializeJson() failed: "));
BLINKER_LOG("没有获取到天气信息");
return;
}
JsonArray doc = jsonBuffer["forecasts"].as<JsonArray>();
dayWeather = doc[0]["dayweather"];
nightWeather = doc[0]["nightweather"];
tempHigh = doc[0]["daytemp"];
tempLow = doc[0]["nighttemp"];
BLINKER_LOG("输出天气信息:");
BLINKER_LOG("dayWeather: ", dayWeather);
BLINKER_LOG("nightWeather: ", nightWeather);
BLINKER_LOG("tempHigh: ", tempHigh);
BLINKER_LOG("tempLow: ", tempLow);
}
解析JSON没有问题。
|