使用心知天气API获取天气信息
时间: 2023-04-10 22:03:07 浏览: 281
可以使用以下代码获取心知天气API的天气信息:
```lua
local http = require("socket.http")
local ltn12 = require("ltn12")
local json = require("json")
local url = "https://api.seniverse.com/v3/weather/now.json?key=your_key&location=your_location"
local response_body = {}
local res, code, response_headers = http.request{
url = url,
method = "GET",
headers = {
["Content-Type"] = "application/json",
},
sink = ltn12.sink.table(response_body),
}
if code == 200 then
local response = table.concat(response_body)
local data = json.decode(response)
local weather = data.results[1].now.text
print("当前天气:" .. weather)
else
print("获取天气信息失败")
end
```
请注意,上述代码中的 `your_key` 和 `your_location` 需要替换为你自己的心知天气API的密钥和城市名称。