Solved JSON parsing with QtJson...
-
Hi
{ "aircraft":[ { "geography":{ "latitude":42.6581, "longitude":5.51009, "altitude":11585.4, "direction":16 }, "speed":{ "horizontal":861.641, "isGround":0, "vertical":0 }, "departure":{ "iataCode":"DJE", "icaoCode":"DTTJ" }, "arrival":{ "iataCode":"ORY", "icaoCode":"LFPO" }, "aircraft":{ "regNumber":"FGZHS", "icaoCode":"B738", "icao24":"3964F2", "iataCode":"B738" }, "airline":{ "iataCode":"TO", "icaoCode":"TVF" }, "flight":{ "iataNumber":"TO3875", "icaoNumber":"TVF3875", "number":"3875" }, "system":{ "updated":"1543087861", "squawk":"7376" }, "status":"started" } ] }
Would obj.aircraft[0].geography
not be an object ? with keys latitude,longitude etc ? -
Firstly, what is this language? JS?
Secondly, what isJSON
in your code and where does it come from? -
@kshegunov Hello,
Yes, it's JS language to get the network response.
the JSON response is come from http://aviation-edge.com
with request like http://aviation-edge.com/v2/public/flights?key=2a8619-db5a5c®Num=ECMJT
-
I am now completely confused. How and why is this JS executed and what relation does it bear to Qt? Is this a QML function?
-
@kshegunov Hi, it has no link with qt, it's just in case some one can help me to parse JSON with keys.
But did what I need parsing in JS but it's not very beautiful code like it is example:
res=doc.responseText//JS.data(doc.responseText) //console.log(res) //var obj =JSON.parse(res) var error="{\"error\": \"No Record Found or Flight not currently detected by receivers. \" }" if(res!==error) { var res1=res.replace("[","") var res2=res1.replace("{","") var res3=res2.split("{") var res4=res3[1] var res5= res4.split(",") var res6=res5[0]+res5[1]+res5[2]+res5[3] var res7=res6 var res8=res7.replace("\n","") var res9=res8.replace(" ","") var res10=res9.replace("\"","") var res11=res10.split(":") var res12=res11[0].replace("\"","") //text: latitude var res13=res11[1].split("\n") var res14=parseFloat(res13[0]) //valeur latitude var res15=res13[1].replace(" \"","") var res16=res15.replace("\"","") //text longitude var res17=parseFloat(res11[2].split("\n")[0])// valeur longitude var res18=parseInt(parseFloat(res11[3].split("\n")[0])*3.28084/100) // valeur altitude var res19 = parseInt(res11[4].split("\n")[0]) // valeur cap
-
@filipdns
It is not a valid json file. https://forum.qt.io/topic/64314/json-access-data-in-multidimensional-arrays/8 -
@Taz742 thank you, I will stay with my poor code then lol
-
-
@aha_1980 Hello, I start to read yes, I'm trying to understand how do that in js, I found documentation on QJsonArray only for c++ at the moment
-
In JS you just parse it, as it's a natively supported.
var myJson = JSON.eval(jsonText);
If it's a valid json, then properties are available directly as any other java script object, or in your case:
var obj =JSON.parse(res); console.log("Latitude: " + obj.aircraft[0].geography.latitude);
I still don't get the relevance, though.
-
This post is deleted!