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
Forum Updated to NodeBB v4.3 + New Features

JSon Parsing

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.4k Views 1 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    use toObject instead of toVariantMap

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    2
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #3

      Hi
      Why you need to convert to
      Object.toVariantMap();

      If you take

        qDebug() << jObject["message"] <<  jObject["__class__"];
      

      Its not the values?

      See small sample here
      https://forum.qt.io/topic/74715/work-with-json-format/3

      Its parsing
      "[ { "Name" : "Bob", "Phonenumber" : 123 },
      { "Name" : "Alice", "Phonenumber" : 321 } ]"

      so almost the same :)

      J 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        Why you need to convert to
        Object.toVariantMap();

        If you take

          qDebug() << jObject["message"] <<  jObject["__class__"];
        

        Its not the values?

        See small sample here
        https://forum.qt.io/topic/74715/work-with-json-format/3

        Its parsing
        "[ { "Name" : "Bob", "Phonenumber" : 123 },
        { "Name" : "Alice", "Phonenumber" : 321 } ]"

        so almost the same :)

        J Offline
        J Offline
        jemazter
        wrote on last edited by
        #4

        @mrjj said in JSon Parsing:

        Hi
        Why you need to convert to
        Object.toVariantMap();

        If you take

          qDebug() << jObject["message"] <<  jObject["__class__"];
        

        Its not the values?

        See small sample here
        https://forum.qt.io/topic/74715/work-with-json-format/3

        Its parsing
        "[ { "Name" : "Bob", "Phonenumber" : 123 },
        { "Name" : "Alice", "Phonenumber" : 321 } ]"

        so almost the same :)

        Hi.

        I'm converting it to VariantMap because i was following this http://blog.linux4us.org/2013/09/05/parse-json-with-qjson-in-qt5/. The data i have is like the following: https://paste2.org/yIBBGm8p

        I think the difference between my data and the example you posted is that in my data the value i want to read ("value") is "inside" "case_package", that is inside "specs" that is inside "results".

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #5

          Edit2

          I corrected the code. You did not realise some of the values were arrays and that there was an "items" object in-between.

          You can use tools like http://jsonviewer.stack.hu/ to have a clearer view of what you are looking at

          void MainWindow::octopartData(QByteArray data)
          {
              const QJsonDocument temp = QJsonDocument::fromJson(data);
              const QJsonObject jObject = temp.object();
              const QJsonArray results = jObject["results"].toArray();
          
              for(const QJsonValue& res : results){
                  const QJsonObject result= res.toObject();
                  const QJsonArray items = result["items"].toArray();
                  for(const QJsonValue& ite : items){
                      const QJsonObject item= ite.toObject();
                      const QJsonObject specs = item["specs"].toObject();
                      const QJsonObject case_package = specs["case_package"].toObject();
                      const QJsonArray value = case_package["value"].toArray();
                      for(auto& val : value){
                          qDebug() << val;
                      }
                  }
              }
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          J 1 Reply Last reply
          3
          • VRoninV VRonin

            Edit2

            I corrected the code. You did not realise some of the values were arrays and that there was an "items" object in-between.

            You can use tools like http://jsonviewer.stack.hu/ to have a clearer view of what you are looking at

            void MainWindow::octopartData(QByteArray data)
            {
                const QJsonDocument temp = QJsonDocument::fromJson(data);
                const QJsonObject jObject = temp.object();
                const QJsonArray results = jObject["results"].toArray();
            
                for(const QJsonValue& res : results){
                    const QJsonObject result= res.toObject();
                    const QJsonArray items = result["items"].toArray();
                    for(const QJsonValue& ite : items){
                        const QJsonObject item= ite.toObject();
                        const QJsonObject specs = item["specs"].toObject();
                        const QJsonObject case_package = specs["case_package"].toObject();
                        const QJsonArray value = case_package["value"].toArray();
                        for(auto& val : value){
                            qDebug() << val;
                        }
                    }
                }
            }
            
            J Offline
            J Offline
            jemazter
            wrote on last edited by
            #6

            @VRonin said in JSon Parsing:

            Edit2

            I corrected the code. You did not realise some of the values were arrays and that there was an "items" object in-between.

            You can use tools like http://jsonviewer.stack.hu/ to have a clearer view of what you are looking at

            void MainWindow::octopartData(QByteArray data)
            {
                const QJsonDocument temp = QJsonDocument::fromJson(data);
                const QJsonObject jObject = temp.object();
                const QJsonArray results = jObject["results"].toArray();
            
                for(const QJsonValue& res : results){
                    const QJsonObject result= res.toObject();
                    const QJsonArray items = result["items"].toArray();
                    for(const QJsonValue& ite : items){
                        const QJsonObject item= ite.toObject();
                        const QJsonObject specs = item["specs"].toObject();
                        const QJsonObject case_package = specs["case_package"].toObject();
                        const QJsonArray value = case_package["value"].toArray();
                        for(auto& val : value){
                            qDebug() << val;
                        }
                    }
                }
            }
            

            Hello.

            Thanks, it's working now. Sorry for the newbie question, but what the 3 '"fors" are doing exactly?

            And just one more question, if inside "items" i have "0" and "1", how can i select one of these itens?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #7

              the answers are related:

              but what the 3 '"fors" are doing exactly?

              they iterate over every item of the array

              if inside "items" i have "0" and "1", how can i select one of these itens?

              instead of iterating over all of them (with the for) you can use at() to select a specific one, for example: const QJsonObject item= items.at(0).toObject();

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • J Offline
                J Offline
                jemazter
                wrote on last edited by
                #8

                Thanks a lot, @VRonin.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jemazter
                  wrote on last edited by
                  #9

                  Sorry, for "reviving" the topic, but i have one more doubt.

                  I'm trying to acces the property "mpn" from "itens" using the following code:

                  void MainWindow::octopartData(QByteArray data)
                  {
                      const QJsonDocument temp = QJsonDocument::fromJson(data);
                      const QJsonObject jObject = temp.object();
                      const QJsonArray results = jObject["results"].toArray();
                      for(const QJsonValue& res : results)
                  {
                          const QJsonObject result= res.toObject();
                          const QJsonArray items = result["items"].toArray();
                          const QJsonObject item = items.at(0).toObject();
                          const QJsonArray mpn = item["mpn"].toArray();
                          qDebug() << mpn;
                      }
                  }
                  

                  But i only get an empty array. Did i did something wrong when accessing the data?

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Hi
                    "mpn": "SN74S74N",
                    Do not look like array to me.
                    Looks like a value.
                    http://doc.qt.io/qt-5/qjsonvalue.html#details
                    "The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. "

                    so try the type() and see what you got :)

                    J 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      Hi
                      "mpn": "SN74S74N",
                      Do not look like array to me.
                      Looks like a value.
                      http://doc.qt.io/qt-5/qjsonvalue.html#details
                      "The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. "

                      so try the type() and see what you got :)

                      J Offline
                      J Offline
                      jemazter
                      wrote on last edited by jemazter
                      #11

                      @mrjj said in JSon Parsing:

                      Hi
                      "mpn": "SN74S74N",
                      Do not look like array to me.
                      Looks like a value.
                      http://doc.qt.io/qt-5/qjsonvalue.html#details
                      "The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. "

                      so try the type() and see what you got :)

                      It was a string after all. Now my code is working as bellow:

                      void MainWindow::octopartData(QByteArray data)
                      {
                          const QJsonDocument temp = QJsonDocument::fromJson(data);
                          const QJsonObject jObject = temp.object();
                          const QJsonArray results = jObject["results"].toArray();
                          for(const QJsonValue& res : results)
                      {
                              const QJsonObject result= res.toObject();
                              const QJsonArray items = result["items"].toArray();
                              const QJsonObject item = items.at(0).toObject();
                              const QString mpn = item["mpn"].toString();
                              qDebug() << mpn;
                          }
                      }
                      

                      Thanks a lot for the help.

                      1 Reply Last reply
                      1

                      • Login

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