实现一个最简单最基本的指定城市查询天气功能

1.首先,你需要注册一个OpenWeatherMap账号,获取一个API密钥

  1. 安装axios来处理HTTP请求。

  2. ​
    npm install axios
    pnpm install axios
    yarn axios
    
    ​

    2.在Vue组件中使用axios发送请求并获取天气数据

    1. <div>
                  <p v-if="weatherData">
                    <strong>城市:</strong> {{ weatherData.name }}, {{ weatherData.sys.country }}<br>
                    <strong>温度:</strong> {{ Math.round(weatherData.main.temp) }}°C<br>
                    <strong>气压:</strong> {{ weatherData.main.pressure }} hPa<br>
                    <strong>湿度:</strong> {{ weatherData.main.humidity }} %<br>
                    <strong>天气:</strong> {{ weatherData.weather[0].description }}
                  </p>
                  <p v-else>
                    加载中...
                  </p>
                </div>
    ts代码
  3. import { onMounted, ref } from 'vue'
    import axios from 'axios'
    
    const weatherData = ref(null)
    onMounted(async () => {
      try {
        const apiKey = 'APIKEY' // 替换为你的API密钥
        const url = `http://api.openweathermap.org/data/2.5/weather?q=安康&appid=${apiKey}&units=metric`
        const response = await axios.get(url)
        weatherData.value = response.data
      }
      catch (error) {
        console.error('Error fetching weather:', error)
      }
    })

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Qt代码示例,它使用了Qt的网络模块来获取指定城市天气信息,并使用QJsonDocument解析返回的JSON数据: ```cpp #include <QtWidgets/QApplication> #include <QtWidgets/QLabel> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include <QJsonDocument> #include <QJsonObject> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建一个 QLabel 来显示天气信息 QLabel label; label.setText("请稍等..."); // 创建一个 QNetworkAccessManager 对象来发送网络请求 QNetworkAccessManager networkManager; // 指定查询城市名称和 API 地址 QString cityName = "北京"; QString apiUrl = "http://wthrcdn.etouch.cn/weather_mini?city=" + cityName; // 发送 GET 请求获取天气数据 QNetworkReply *reply = networkManager.get(QNetworkRequest(QUrl(apiUrl))); // 当请求完成时,解析返回的 JSON 数据并更新 QLabel QObject::connect(reply, &QNetworkReply::finished, [&]() { QByteArray data = reply->readAll(); QJsonDocument jsonDoc = QJsonDocument::fromJson(data); QJsonObject jsonObj = jsonDoc.object(); // 从 JSON 数据中提取天气信息 QString city = jsonObj.value("data").toObject().value("city").toString(); QString weather = jsonObj.value("data").toObject().value("forecast").toArray()[0].toObject().value("type").toString(); QString temperature = jsonObj.value("data").toObject().value("wendu").toString(); // 更新 QLabel 显示天气信息 label.setText(QString("%1\n天气:%2\n温度:%3℃").arg(city).arg(weather).arg(temperature)); }); label.show(); return app.exec(); } ``` 请注意,这是一个非常简单的示例,仅用于演示如何使用 Qt 和 JSON 解析器来获取并显示天气信息。实际应用中,您需要考虑处理网络请求中的错误、处理各种 JSON 数据格式、更新 UI 界面的性能等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值