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 8.9k 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.
  • 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