我正在尝试在 Qt 中制作一个小型天气应用程序,我正在使用 JSON 格式的世界天气在线 API。我只是制作了一个简单的文本部分并尝试打印从 JSON 提要中接收到的数据。导入 QtQuick 2.0
Rectangle {
TextInput {
width: 240
id: city_label
text: "Chicago"
font.family: "Helvetica"
font.pointSize: 12
color: "#000000"
focus: true
}
function abc()
{
var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState == XMLHttpRequest.DONE) {
var jsonObject = eval('(' + doc.responseText + ')');
loaded(jsonObject);
}
}
doc.open("GET", "http://free.worldweatheronline.com/feed/weather.ashx?q=" + city_label.text + "&format=json&num_of_days=2&key=640bc6c793043201130202");
doc.send();
}
function showRequestInfo(text) {
log.text = log.text + "\n" + text
console.log(text)
}
function loaded(jsonObject)
{
showRequestInfo("cloud:" + jsonObject.data.current_condition[0].cloudcover);
showRequestInfo("humidity:" + jsonObject.data.current_condition[0].humidity);
showRequestInfo("observation_time:" + jsonObject.data.current_condition[0].observation_time);
showRequestInfo("precipMM:" + jsonObject.data.current_condition[0].precipMM);
showRequestInfo("pressure:" + jsonObject.data.current_condition[0].pressure);
showRequestInfo("temp_C:" + jsonObject.data.current_condition[0].temp_C);
showRequestInfo("temp_F:" + jsonObject.data.current_condition[0].temp_F);
showRequestInfo("visibility:" + jsonObject.data.current_condition[0].visibility);
showRequestInfo("weatherCode:" + jsonObject.data.current_condition[0].weatherCode);
showRequestInfo("weatherDesc:" + jsonObject.data.current_condition[0].weatherDesc[0].value);
showRequestInfo("weatherIconUrl:" + jsonObject.data.current_condition[0].weatherIconUrl[0].value);
image1.source = jsonObject.data.current_condition[0].weatherIconUrl[0].value;
showRequestInfo("winddir16Point:" + jsonObject.data.current_condition[0].winddir16Point);
showRequestInfo("winddirDegree:" + jsonObject.data.current_condition[0].winddirDegree);
showRequestInfo("windspeedKmph:" + jsonObject.data.current_condition[0].windspeedKmph);
showRequestInfo("windspeedMiles:" + jsonObject.data.current_condition[0].windspeedMiles);
showRequestInfo("Location:" + jsonObject.data.request[0].query);
}
width: 800
height: 1280
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
id: log
width: 360
anchors.top: parent.top;
anchors.bottom: parent.bottom;
anchors.margins: 10
}
}