Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. JSON parsing with QtJson...
Forum Updated to NodeBB v4.3 + New Features

JSON parsing with QtJson...

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 3.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    filipdns
    wrote on 24 Nov 2018, 13:44 last edited by kshegunov
    #1

    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]

    T 1 Reply Last reply 25 Nov 2018, 20:37
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 24 Nov 2018, 16:05 last edited by
      #2

      It seems I've moved this in error.
      What is QtJson and where it comes from, and why are you using that instead of Qt's own facilities ...?

      Read and abide by the Qt Code of Conduct

      F 1 Reply Last reply 24 Nov 2018, 16:10
      0
      • K kshegunov
        24 Nov 2018, 16:05

        It seems I've moved this in error.
        What is QtJson and where it comes from, and why are you using that instead of Qt's own facilities ...?

        F Offline
        F Offline
        filipdns
        wrote on 24 Nov 2018, 16:10 last edited by
        #3

        @kshegunov Hello,

        I use it because I found only readable example on this.

        May be I can use something else but I don't know how

        K 1 Reply Last reply 24 Nov 2018, 16:12
        0
        • F filipdns
          24 Nov 2018, 16:10

          @kshegunov Hello,

          I use it because I found only readable example on this.

          May be I can use something else but I don't know how

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 24 Nov 2018, 16:12 last edited by
          #4

          My point was Qt already has support for JSON, so I have no idea why you pulled an additional dependency is all.

          Read and abide by the Qt Code of Conduct

          F 2 Replies Last reply 24 Nov 2018, 16:13
          2
          • K kshegunov
            24 Nov 2018, 16:12

            My point was Qt already has support for JSON, so I have no idea why you pulled an additional dependency is all.

            F Offline
            F Offline
            filipdns
            wrote on 24 Nov 2018, 16:13 last edited by
            #5

            @kshegunov thanks a lot, I will check on it

            1 Reply Last reply
            0
            • K kshegunov
              24 Nov 2018, 16:12

              My point was Qt already has support for JSON, so I have no idea why you pulled an additional dependency is all.

              F Offline
              F Offline
              filipdns
              wrote on 24 Nov 2018, 18:52 last edited by filipdns
              #6

              @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&regNum=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...

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 24 Nov 2018, 19:03 last edited by
                #7

                Because it's inside an array:

                [ ... ]

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                F 1 Reply Last reply 24 Nov 2018, 19:09
                1
                • C Christian Ehrlicher
                  24 Nov 2018, 19:03

                  Because it's inside an array:

                  [ ... ]

                  F Offline
                  F Offline
                  filipdns
                  wrote on 24 Nov 2018, 19:09 last edited by filipdns
                  #8

                  @Christian-Ehrlicher thanks, then, what I have to do to extract the data what I need?...

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 24 Nov 2018, 19:14 last edited by
                    #9

                    @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

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    F 1 Reply Last reply 24 Nov 2018, 19:39
                    1
                    • C Christian Ehrlicher
                      24 Nov 2018, 19:14

                      @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

                      F Offline
                      F Offline
                      filipdns
                      wrote on 24 Nov 2018, 19:39 last edited by
                      #10

                      @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]...

                      K 1 Reply Last reply 25 Nov 2018, 12:16
                      0
                      • C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 25 Nov 2018, 07:15 last edited by Christian Ehrlicher
                        #11

                        Sorry but I don't understand python that much to help you.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 25 Nov 2018, 08:01 last edited by
                          #12

                          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 ?

                          1 Reply Last reply
                          0
                          • F filipdns
                            24 Nov 2018, 19:39

                            @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]...

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 25 Nov 2018, 12:16 last edited by
                            #13

                            Firstly, what is this language? JS?
                            Secondly, what is JSON in your code and where does it come from?

                            Read and abide by the Qt Code of Conduct

                            F 1 Reply Last reply 25 Nov 2018, 16:09
                            0
                            • K kshegunov
                              25 Nov 2018, 12:16

                              Firstly, what is this language? JS?
                              Secondly, what is JSON in your code and where does it come from?

                              F Offline
                              F Offline
                              filipdns
                              wrote on 25 Nov 2018, 16:09 last edited by
                              #14

                              @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&regNum=ECMJT

                              K 1 Reply Last reply 25 Nov 2018, 18:28
                              0
                              • F filipdns
                                25 Nov 2018, 16:09

                                @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&regNum=ECMJT

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 25 Nov 2018, 18:28 last edited by
                                #15

                                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?

                                Read and abide by the Qt Code of Conduct

                                F 1 Reply Last reply 25 Nov 2018, 18:44
                                0
                                • K kshegunov
                                  25 Nov 2018, 18:28

                                  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?

                                  F Offline
                                  F Offline
                                  filipdns
                                  wrote on 25 Nov 2018, 18:44 last edited by
                                  #16

                                  @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
                                  
                                  1 Reply Last reply
                                  0
                                  • F filipdns
                                    24 Nov 2018, 13:44

                                    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]

                                    T Offline
                                    T Offline
                                    Taz742
                                    wrote on 25 Nov 2018, 20:37 last edited by
                                    #17

                                    @filipdns
                                    It is not a valid json file. https://forum.qt.io/topic/64314/json-access-data-in-multidimensional-arrays/8

                                    Do what you want.

                                    F 1 Reply Last reply 25 Nov 2018, 21:23
                                    3
                                    • T Taz742
                                      25 Nov 2018, 20:37

                                      @filipdns
                                      It is not a valid json file. https://forum.qt.io/topic/64314/json-access-data-in-multidimensional-arrays/8

                                      F Offline
                                      F Offline
                                      filipdns
                                      wrote on 25 Nov 2018, 21:23 last edited by
                                      #18

                                      @Taz742 thank you, I will stay with my poor code then lol

                                      aha_1980A 1 Reply Last reply 25 Nov 2018, 21:26
                                      0
                                      • F filipdns
                                        25 Nov 2018, 21:23

                                        @Taz742 thank you, I will stay with my poor code then lol

                                        aha_1980A Offline
                                        aha_1980A Offline
                                        aha_1980
                                        Lifetime Qt Champion
                                        wrote on 25 Nov 2018, 21:26 last edited by
                                        #19

                                        @filipdns

                                        Did you read the post @Taz742 linked?

                                        Your top-level structure is a JSON array, so you must convert the document to a QJsonArray, not a QJsonObject.

                                        Using a QJsonArray should work for you, too.

                                        Qt has to stay free or it will die.

                                        F 1 Reply Last reply 26 Nov 2018, 15:30
                                        1
                                        • aha_1980A aha_1980
                                          25 Nov 2018, 21:26

                                          @filipdns

                                          Did you read the post @Taz742 linked?

                                          Your top-level structure is a JSON array, so you must convert the document to a QJsonArray, not a QJsonObject.

                                          Using a QJsonArray should work for you, too.

                                          F Offline
                                          F Offline
                                          filipdns
                                          wrote on 26 Nov 2018, 15:30 last edited by filipdns
                                          #20

                                          @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

                                          K 1 Reply Last reply 26 Nov 2018, 16:40
                                          0

                                          8/22

                                          24 Nov 2018, 19:09

                                          14 unread
                                          • Login

                                          • Login or register to search.
                                          8 out of 22
                                          • First post
                                            8/22
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved