JSON形式の世界天気オンラインAPIを使用しているQtで小さな天気アプリを作成しようとしています.単純なテキストセクションを作成し、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
}
}