gpt4 book ai didi

javascript - 在 Qt 中使用 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 05:51:18 25 4
gpt4 key购买 nike

我正在尝试在 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

}

最佳答案

与在网络浏览器中一样,您可以在 QML 代码中访问 native JSON.parse 和 JSON.serialize。

关于javascript - 在 Qt 中使用 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14699050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com