JSON parsing with QtJson...
-
Hello,
I'm trying to parse value from JSON file:
[ { "geography": { "latitude": 51.1392, "longitude": 11.1481, "altitude": 11280.5, "direction": 68.9 }, "speed": { "horizontal": 805.37, "isGround": 0, "vertical": -3.90144 }, "departure": { "iataCode": "BVA", "icaoCode": "LFOB" }, "arrival": { "iataCode": "CIA", "icaoCode": "LIRA" }, "aircraft": { "regNumber": "EIGDT", "icaoCode": "B738", "icao24": "", "iataCode": "B738" }, "airline": { "iataCode": "FR", "icaoCode": "RYR" }, "flight": { "iataNumber": "FR92QB", "icaoNumber": "RYR92QB", "number": "92QB" }, "system": { "updated": "1543065538", "squawk": "1127" }, "status": "en-route" }, { "geography": { "latitude": 43.4357, "longitude": 18.9397, "altitude": 11280.5, "direction": 136 }, "speed": { "horizontal": 914.949, "isGround": 0, "vertical": -5.20192 }, "departure": { "iataCode": "BVA", "icaoCode": "LFOB" }, "arrival": { "iataCode": "SKG", "icaoCode": "LGTS" }, "aircraft": { "regNumber": "EIEKI", "icaoCode": "B738", "icao24": "4CA808", "iataCode": "B738" }, "airline": { "iataCode": "FR", "icaoCode": "RYR" }, "flight": { "iataNumber": "FR3094", "icaoNumber": "RYR3094", "number": "3094" }, "system": { "updated": "1543065479", "squawk": "664" }, "status": "en-route" } ]
with
QByteArray data= reply->readAll(); QString datastring = QString::fromStdString(data.toStdString()); //qDebug()<<data; bool ok; // datastring is a QString containing the JSON data QtJson::JsonObject result = QtJson::parse(datastring, ok).toMap(); if(!ok) { qFatal("An error occurred during parsing"); } qDebug() << "latitude:" << result["latitude"].toString();
but the result say only:
latitude: ""
What I did wrong?
[Moved to General and Desktop ~kshegunov]
-
It seems I've moved this in error.
What isQtJson
and where it comes from, and why are you using that instead of Qt's own facilities ...? -
My point was Qt already has support for JSON, so I have no idea why you pulled an additional dependency is all.
-
@kshegunov Hello, I try following support for JSON but doesn't work...
json file:
{ "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"
}
]}function getFlight(registration) { var res var url = 'http://aviation-edge.com/v2/public/flights?key=2a8619-db5a5c®Num=FGZHS' var doc= new XMLHttpRequest() doc.onreadystatechange = function(){ if(doc.readyState == XMLHttpRequest.DONE){ res=doc.responseText console.log(res) var obj =JSON.parse(res) console.log(obj.geography) } } doc.open('GET', url, true) doc.send() }
with console.log(res) I display the correct content from file
but with console.log(obj.geography) I receive undefined on console... -
Because it's inside an array:
[ ... ]
-
@Christian-Ehrlicher thanks, then, what I have to do to extract the data what I need?...
-
@filipdns said in JSON parsing with QtJson...:
what I have to do to extract the data what I need?...
Extract the array I would guess... http://doc.qt.io/qt-5/qjsonarray.html
-
@Christian-Ehrlicher I try to work with it like array:
var obj =JSON.parse(res)
console.log(obj.aircraft[0].geography)but the return message is [object Object]...
-
Sorry but I don't understand python that much to help you.
-
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 ? -
@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
-
@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