Parse json to listview in qml -
i'm writing small application in qml shows weather details in listview. can't information on how parse complex json. i'm trying parse in qml. json:
{ "coord":{ "lon":-0.13, "lat":51.51 }, "weather":[ { "id":520, "main":"rain", "description":"light intensity shower rain", "icon":"09d" }, { "id":310, "main":"drizzle", "description":"light intensity drizzle rain", "icon":"09d" } ], "base":"cmc stations", "main":{ "temp":285.33, "pressure":1006, "humidity":82, "temp_min":284.15, "temp_max":286.15 }, "wind":{ "speed":7.7, "deg":210, "gust":12.9 }, "rain":{ "1h":1.4 }, "clouds":{ "all":75 }, "dt":1453904502, "sys":{ "type":1, "id":5091, "message":0.0047, "country":"gb", "sunrise":1453880766, "sunset":1453912863 }, "id":2643743, "name":"london", "cod":200 }
i tried code it's not working. in code send http request, try parse json , show listview.
import qtquick 2.0 rectangle { id: main width: 320 height: 640 color: 'skyblue' listmodel { id: listmodeljson } rectangle { height: parent.height width: parent.width listview { id: listviewjson x: 0 y: 0 width: 600 height: 592 delegate: rectangle { width: parent.width height: 70 } model: listmodeljson } } function getcityname() { var request = new xmlhttprequest() request.open('get', 'http://api.openweathermap.org/data/2.5/weather?q=london&appid=44db6a862fba0b067b1930da0d769e98', true); request.onreadystatechange = function() { if (request.readystate === xmlhttprequest.done) { if (request.status && request.status === 200) { console.log("response", request.responsetext) var result = json.parse(request.responsetext) (var in result) { listmodeljson.append({ "name" : result[i].name, "cod" : result[i].cod }); } // main.cityname = result.response } else { console.log("http:", request.status, request.statustext) } } } request.send() } component.oncompleted: { getcityname() } }
can show me way can parse json?
found question google , done documentation listmodel qml element.
maybe else finds useful:
code:
import qtquick 2.0 rectangle { width: 400 height: 200 listmodel { id: citymodel listelement { name: "static sunny city" temp: 31.95 attributes: [ listelement { description: "tropical" }, listelement { description: "cloudless" } ] } } component { id: citydelegate row { spacing: 10 text { text: name } text { text: temp + "°c" } } } listview { anchors.fill: parent model: citymodel delegate: citydelegate } component.oncompleted: { citymodel.append({"name": "append cold city", "temp": 5.95}) getcityjson() } function getcityjson() { var request = new xmlhttprequest() request.open('get', 'http://api.openweathermap.org/data/2.5/weather?q=london&units=metric&appid=44db6a862fba0b067b1930da0d769e98', true); request.onreadystatechange = function() { if (request.readystate === xmlhttprequest.done) { if (request.status && request.status === 200) { console.log("response", request.responsetext) var result = json.parse(request.responsetext) citymodel.append({ "name": result.name + " " + date(result.dt * 1000), "temp": result.main.temp }) } else { console.log("http:", request.status, request.statustext) } } } request.send() } }
Comments
Post a Comment