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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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

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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      0
      • kshegunovK kshegunov

        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 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

        kshegunovK 1 Reply Last reply
        0
        • F filipdns

          @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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          0
          • kshegunovK kshegunov

            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 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

              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]

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on 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
              3
              • Taz742T Taz742

                @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 last edited by
                #18

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

                aha_1980A 1 Reply Last reply
                0
                • F filipdns

                  @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 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
                  1
                  • aha_1980A aha_1980

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

                    kshegunovK 1 Reply Last reply
                    0
                    • F filipdns

                      @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

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #21

                      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.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2
                      • M Offline
                        M Offline
                        makcorps
                        Banned
                        wrote on last edited by
                        #22
                        This post is deleted!
                        1 Reply Last reply
                        0

                        • Login

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