2
我想在Qt中製作一個小型的天氣應用程序,我正在使用JSON格式的世界天氣在線API。我只是做了一個簡單的文本部分,並試圖打印來自接收的數據JSON供稿。 導入QtQuick 2.0在Qt中使用JSON對象
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
}
}
如果你使用Qt5,你應該使用Qt JSON(包含在Qt Core中)而不是qjson(外部庫)。 –