Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved JSon Parsing

    General and Desktop
    3
    11
    3800
    Loading More Posts
    • 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.
    • J
      jemazter last edited by

      Hello.

      I developing a application that makes a request to an server and i get the following response (https://octopart.com/api/v3/parts/match?apikey=EXAMPLE_KEY&queries=[{"mpn":"SN74S74N"}]&pretty_print=true&include[]=specs&include[]=specs). I need to access the variable "results">"specs">"case_package">"value". This is my code:

      void MainWindow::octopartData(QByteArray data)
      {
          QString dataString;
          dataString = data;
          // ui->textBrowser->append(data); //DEBUG PURPOSES
          QJsonDocument temp = QJsonDocument::fromJson(data);
          QJsonObject jObject = temp.object();
          QVariantMap mainMap = jObject.toVariantMap();
          QVariantMap results = mainMap["results"].toMap();
          QVariantMap specs = results["specs"].toMap();
          QVariantMap case_package = specs["case_package"].toMap();
          qDebug()<<case_package["value"];
      }
      

      But is not working, debug is showing only QVariant(Invalid). I would like to get some advice to solve this problem.

      Thanks in advance.

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

        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 Reply Quote 2
        • mrjj
          mrjj Lifetime Qt Champion last edited by 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 1 Reply Last reply Reply Quote 1
          • J
            jemazter @mrjj last edited by

            @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 Reply Quote 0
            • VRonin
              VRonin last edited by 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;
                          }
                      }
                  }
              }
              

              "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 Reply Quote 3
              • J
                jemazter @VRonin last edited by

                @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 Reply Quote 0
                • VRonin
                  VRonin last edited by

                  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 Reply Quote 3
                  • J
                    jemazter last edited by

                    Thanks a lot, @VRonin.

                    1 Reply Last reply Reply Quote 0
                    • J
                      jemazter last edited by

                      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 Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion last edited by

                        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 Reply Quote 2
                        • J
                          jemazter @mrjj last edited by jemazter

                          @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 Reply Quote 1
                          • First post
                            Last post