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. [Solved] Parsing a JSon reply issue
Forum Updated to NodeBB v4.3 + New Features

[Solved] Parsing a JSon reply issue

Scheduled Pinned Locked Moved General and Desktop
23 Posts 3 Posters 9.1k 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #6

    Sounds like you are not using C++/11, which is needed for this code to work.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      roseicollis
      wrote on last edited by
      #7

      Yes, that was the first error i solved. I put it on the .pro file as

      QMAKE_CXXFLAGS += -std=c++11
      and tried too with
      QMAKE_CXXFLAGS += -std=gnu++11

      And the compile output gaves something like:

      @
      g++ -c -pipe -std=c++11 -g -Wall -W -D_REENTRANT -DQT_SCRIPT_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib/qt4/mkspecs/linux-g++[...]@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #8

        Did you try with simply putting
        @
        CONFIG += c++11
        @
        in your .pro file?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          roseicollis
          wrote on last edited by
          #9

          Then I get the same error I had first:

          /usr/lib/gcc/i686-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

          Which I solved with
          @
          QMAKE_CXXFLAGS = -std=c++11

          (and even with the next line instead of the other the error dissapears)
          QMAKE_CXXFLAGS = -std=gnu+11
          @

          The error I was saying is in line 197:
          @
          for (auto& element : a)
          {
          value_.object->emplace(std::make_pair(std::move(element[0]), std::move(element[1]))); // HERE IS THE ERROR
          }
          @

          1 Reply Last reply
          0
          • R Offline
            R Offline
            roseicollis
            wrote on last edited by
            #10

            I've found this other solution: "JSon":https://github.com/gaudecker/qt-json

            It seems that it works but now I can't get the name and code values, I'm trying to do:

            @
            bool ok;

            QtJson::JsonObject result = QtJson::parse(myJson, ok).toMap();
            if(!ok) {
            qFatal("An error occurred during parsing");
            }else
            {
            qDebug() << "data" << result["data"].toString();
            qDebug() << " try1" << result["Name"].toString();

            QtJson::JsonObject nested = result["data"].toMap();
            qDebug() << "try2:" << nested["Name"].toString();
            

            @

            But try1 and try2 are empy. Any Idea of how to make that work?

            Thanks!

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #11

              Hi,

              [quote author="roseicollis" date="1423496917"]But try1 and try2 are empy. Any Idea of how to make that work?[/quote]Your data is a JSON array, not a JSON object.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • R Offline
                R Offline
                roseicollis
                wrote on last edited by
                #12

                Oh I see...

                But if I put:
                @
                QtJson::JsonArray nested = result["data"].toMap();
                qDebug() << "try2:" << nested["Name"].toString();
                @

                It says:
                @
                conversion from 'QMap<QString, QVariant>' to non-scalar type 'QtJson::JsonArray {aka QList<QVariant>}' requested
                @

                I don't really know how to parse that array... I've tried this too:

                @
                QtJson::JsonObject bbb = QtJson::parse(result["data"].toString(), ok).toMap();
                qDebug() << bbb;
                @
                But it only prints: QMap()

                So... I'm really stuck there... -.-''

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #13

                  Are you familiar with JSON syntax?

                  • represents an array.
                  • { } represents an object.

                  You need to understand your structure first: You have 1 array. That array contains 2 objects. Each object contains 2 strings

                  Extract the objects from the array like this:
                  @
                  auto object1 = result[0];
                  auto object2 = result[1];
                  @

                  After that, you can extract the "Name" and "data" fields from the objects.

                  Remember, arrays are indexed by integers, while objects/maps are indexed by strings.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    roseicollis
                    wrote on last edited by
                    #14

                    Hi JKSH,

                    Ermmm noup, I wasn't familiar with it but now I understand it better :) Thanks!

                    One thing: what is auto for you? O.o a JsonObject?

                    @
                    QtJson::JsonObject object1 = result[0].toMap(); //object1 returns: QMap()
                    QVariant object1 = result[0]; //object1 returns: QVariant(, )
                    QVariant object1 = result[0].toString();//object1 returns: QVariant(QString, )
                    @

                    EDIT: Didn't know there were something called 'auto' O.o but trying that:

                    @
                    QtJson::JsonObject result = QtJson::parse(myJson, ok).toMap();
                    auto object1 = result[0];
                    QtJson::JsonObject trying = QtJson::parse(object1, ok).toMap();
                    qDebug() <<" trying msg" << trying;
                    @

                    it says: error: no matching function for call to 'parse(QVariant&, bool&)'
                    And if I try with
                    @
                    QtJson::JsonObject trying = QtJson::parse(object1["Name"], ok).toMap();
                    @
                    It says: error: no match for 'operator[]' in 'object1["Name"]'

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #15

                      You're welcome. :)

                      [quote author="roseicollis" date="1423567075"]
                      @
                      QtJson::JsonObject result = QtJson::parse(myJson, ok).toMap();
                      @
                      [/quote]2 issues:

                      Like I said before, your data is an array, not an object. So, you should make result a JsonArray, not a JsonObject. Also, toMap() produces an object. You want to use toList() instead, which produces an array.

                      What type is myJson? Your compiler complained because you passed a QVariant into QtJson::parse(). What type does it actually accept? (hint: look inside json.h)

                      [quote author="roseicollis" date="1423567075"]EDIT: Didn't know there were something called 'auto' O.o[/quote]'auto' is a "C++11 keyword":http://geekswithblogs.net/BlackRabbitCoder/archive/2012/03/15/c-little-wonders-the-c11-auto-keyword-redux.aspx. When you use it, the compiler will try to figure out the variable's type automatically.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        roseicollis
                        wrote on last edited by
                        #16

                        Hi again ^^

                        [quote author="JKSH" date="1423568300"]
                        Like I said before, your data is an array, not an object. So, you should make result a JsonArray, not a JsonObject. Also, toMap() produces an object. You want to use toList() instead, which produces an array.
                        [/quote]
                        Didn't know that it creates an object too... (anyway i tried before with strings and bytearray hopeing something works hahaha but not obviously)

                        [quote author="JKSH" date="1423568300"]
                        What type is myJson? Your compiler complained because you passed a QVariant into QtJson::parse(). What type does it actually accept? (hint: look inside json.h)
                        [/quote]

                        myJson is a QString:
                        @
                        QNetworkRequest request(_Url);
                        QNetworkReply *reply = nam->get(request);
                        QString myJson = (QString)reply->readAll();
                        @

                        I know that this error is because of the types but I was thinking that it takes Variants and now saw that not. The definition of parse is:
                        @
                        QVariant parse(const QString &json, bool &success);
                        @

                        If I try with:
                        @
                        QtJson::JsonArray result = QtJson::parse(myJson, ok).toList();
                        qDebug() << " a " << result;
                        auto object1 = result[0];
                        qDebug() << " a1 " << object1;
                        @

                        Then it prints:
                        a()
                        ASSERT failure in QList<T>::operator[]: "index out of range",

                        Which is normal if a() is empty O.o

                        I'll have to inquire about what c++11 is, really thank you so much! Now I have new weekend reading :D

                        1 Reply Last reply
                        0
                        • JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #17

                          Hmm... what do you get if you do this?

                          @
                          qDebug() << myJson;
                          qDebug() << QtJson::parse(myJson, ok);
                          qDebug() << ok;
                          @

                          C++11 is the updated version of C++ which came out in 2011 :) It offers lots of new language features to make C++ more powerful and easier to use. C++14 is in the works, but it's no longer 2014 now so the update will probably be called C++15 instead.

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            roseicollis
                            wrote on last edited by
                            #18

                            This returns the next:

                            @
                            "{"data":"[{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]"}"

                            QVariant(QVariantMap, QMap(("data", QVariant(QString, "[{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]") ) ) )

                            true
                            @

                            wooow I thought there were only C, C# and C++ .... what an inonce girl I am XDDDD I will definitely look more about it at the weekend, it sounds really interesting!

                            1 Reply Last reply
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #19

                              [quote author="roseicollis" date="1423572240"]
                              @
                              "{"data":"[{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]"}"
                              @

                              [/quote]I'm so sorry... I though myJson was
                              @
                              [{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]
                              @

                              ...that's why I thought your data was an array of 2 objects. :( Sorry for sending you on a wild goose chase!

                              Anyway, your actual JSON data is: 1 object which contains 1 string value. I must say your data is formatted quite strangely (where did you get it from?).

                              So, in the first run, you can only extract one string:
                              @
                              QtJson::JsonObject outerObject = QtJson::parse(myJson, ok).toMap();
                              QString innerString = outerObject.toString();

                              qDebug() << myJson;
                              qDebug() << innerString;
                              @

                              You will see that myJson contains lots of '' characters, but innerString doesn't. That's because the data in outerObject is escaped -- the JSON parser treats it as a single string instead of a JSON array.

                              Anyway, after parsing it the first time, you now have un-escaped data in innerString, so you can send it through the JSON parser again to extract the array:

                              @
                              QtJson::JsonArray outerArray = QtJson::parse(innerString, ok).toList();

                              qDebug() << ok;
                              qDebug() << outerArray;
                              @

                              Now, you can apply my previous posts.

                              Good luck!

                              [quote author="roseicollis" date="1423572240"]wooow I thought there were only C, C# and C++ .... what an inonce girl I am XDDDD I will definitely look more about it at the weekend, it sounds really interesting![/quote]There are lots and lots of programming languages out there. :) C, C# and C++ are different languages.

                              However, C++03, C++11, and C++14 are different versions of the same language.

                              Happy reading!

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                roseicollis
                                wrote on last edited by
                                #20

                                [quote author="JKSH" date="1423574139"]
                                . :( Sorry for sending you on a wild goose chase!
                                [/quote]
                                No problem! I learnt a lot with that too :D

                                [quote author="JKSH" date="1423574139"]
                                I must say your data is formatted quite strangely (where did you get it from?)
                                [/quote]

                                I don't know which should be its structure but this is what I will receive from a webservice which is made by a work mate and the only thing I know is: I have to parse it and show it on a widget (Still dunno if combobox or what will be the best option)

                                [quote author="JKSH" date="1423574139"]
                                @
                                QtJson::JsonObject outerObject = QtJson::parse(myJson, ok).toMap();
                                QString innerString = outerObject.toString();

                                qDebug() << myJson;
                                qDebug() << innerString;
                                @
                                [/quote]

                                I had to make some changes to make it work:

                                @
                                QtJson::JsonObject outerObject = QtJson::parse(myJson, ok).toMap();
                                QString innerString = outerObject["data"].toString();
                                qDebug() << myJson;
                                qDebug() << innerString;
                                QtJson::JsonArray outerArray = QtJson::parse(innerString, ok).toList();
                                qDebug() << ok;
                                qDebug() << outerArray;
                                auto object1 = outerArray[0].toMap();
                                qDebug() << " DD" << object1["Name"];
                                @

                                So:

                                • DD returns: DD QVariant(QString, "One")

                                Now I'm trying to get that "One" string alone but at least I can see it!! I'm almost there, so cloose!

                                [quote author="JKSH" date="1423574139"]
                                There are lots and lots of programming languages out there. :) C, C# and C++ are different languages.
                                However, C++03, C++11, and C++14 are different versions of the same language.
                                [/quote]

                                Yes, I know there are soo many programming languages but didn't think about versions.. Never though they exist for the languages too haha

                                1 Reply Last reply
                                0
                                • JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #21

                                  [quote author="roseicollis" date="1423581028"]I had to make some changes to make it work:[/quote]Good catch!

                                  [quote author="roseicollis" date="1423581028"]So:

                                  • DD returns: DD QVariant(QString, "One")

                                  Now I'm trying to get that "One" string alone but at least I can see it!! I'm almost there, so cloose![/quote]Yep, almost there! :) object1["Name"] is a QVariant, so see http://doc.qt.io/qt-5/qvariant.html for clues

                                  [quote author="JKSH" date="1423574139"]Yes, I know there are soo many programming languages but didn't think about versions.. Never though they exist for the languages too haha[/quote]I think Shakespeare's English and modern English are 2 different versions of English ;-P

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    roseicollis
                                    wrote on last edited by
                                    #22

                                    [quote author="JKSH" date="1423582568"]
                                    Yep, almost there! :) object1[“Name”] is a QVariant, so see http://doc.qt.io/qt-5/qvariant.html for clues
                                    [/quote]
                                    Yesterday I was trying with object1["Name"].toString() but it was saying all time that there were no func like toString()... today I tried it again because it sounds logical for me and now it works... -. -'' I really hate programing when that happens hahahaha... anyway, it works :D So I'm happy :D

                                    [quote author="JKSH" date="1423582568"]
                                    I think Shakespeare’s English and modern English are 2 different versions of English ;-P
                                    [/quote]
                                    Wooow, do you really think that?? O.o I thought that the english nowadays was the same as shakespears or as the cavemen.. :P I was just joking ^.^ Haha that is a really really nice example!

                                    Thank you for all your help guys! And special thanks for you JKSH. I have enjoyed so much this problem and learnt a lot! :D

                                    1 Reply Last reply
                                    0
                                    • JKSHJ Offline
                                      JKSHJ Offline
                                      JKSH
                                      Moderators
                                      wrote on last edited by
                                      #23

                                      You're most welcome :)

                                      Happy coding!

                                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                      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