Results 1 to 13 of 13

Thread: Parsing a JSon webservice reply issue

  1. #1
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Parsing a JSon webservice reply issue

    Hi,

    I’m doing a request to an url which gives me the next Json reply:

    Qt Code:
    1. "{"data":"[{\"Name\":\"One\",\"Code\":\"\"},{\"Name\":\"Two\",\"Code\":\"Alpha\"}]"}"
    To copy to clipboard, switch view to plain text mode 
    I am using Qt 8.4.5 so I have to do it with QNetwork and QScript. So following an example I saw (Example)I did the next:
    Qt Code:
    1. QNetworkRequest request(_Url);
    2. QNetworkReply *reply = nam->get(request);
    3.  
    4. QString InfoByteArray = (QString)reply->readAll();
    5.  
    6. if ( reply->error()== QNetworkReply::NoError)
    7. {
    8. //------------ JSON Parser --------------//
    9.  
    10. QScriptEngine engine;
    11. QScriptValue result = engine.evaluate("("+InfoByteArray+")");
    12.  
    13. QScriptValue InfoData= result.property("data");
    14. qDebug() << "InfoData" << InfoData.toString();
    15. }
    To copy to clipboard, switch view to plain text mode 

    With that I get all the part on the right of “data”:

    Qt Code:
    1. [{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]"
    To copy to clipboard, switch view to plain text mode 
    Now I want to take the Name en Code values ofc but I can’t get them, I try doing this but never goes into the while:

    Qt Code:
    1. QScriptValueIterator it(InfoData);
    2. while (it.hasNext())
    3. {
    4. it.next();
    5. QScriptValue entry = it.value();
    6.  
    7. QString name= entry.property("Name").toString(); //this doesnt works
    8. QString name2= entry.property("data").property("Name").toString(); //this either
    9. }
    To copy to clipboard, switch view to plain text mode 
    And if I try doing that out of the while, I just get empty strings :
    Qt Code:
    1. QString name= result.property("Name").toString(); // qDebug() << name; returns ""
    2. QString name2= entry.property("data").property("Name").toString();
    To copy to clipboard, switch view to plain text mode 
    So…Any Idea of what am I doing wrong here so I can't get the Name or the Code values??

    Thank you!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Parsing a JSon webservice reply issue

    The JSON data you present is a single object with one key "data" and a string value. That outer object does not contain any other key/value pairs or subordiante object structures.

    If you want to parse the "data" string as JSON then you will need to pass that value through the QScriptEngine (Or QJson) to get an array of objects each with Name and Code keys.

  3. #3
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing a JSon webservice reply issue

    Hi ChrisW67,

    Thanks for your reply.
    If you want to parse the "data" string as JSON
    Well in fact what I need is the information. I don't care about if its a Json or not.

    pass that value through the QScriptEngine (Or QJson)
    JSon is not possible because its for Qt5+ and I'm working on Qt4.8.5 (I just noticed I put it wrong upside.. of course its not 8.4.5 XD)
    So it has to be through QScriptEngine but I tried so many different ways and I couldn't get it, could you gave me an example please?

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing a JSon webservice reply issue

    QJson is an external library for Qt4 for parsing JSON data. Regarding QScriptEngine, Chris already told you what to do - pass the string representing the value through the parser again. To be honest it is kind of weird that the service stringifies the value instead of passing it directly in the JSON document.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing a JSon webservice reply issue

    Oh! Sorry I didn't understand it well then.. I didn't know there were external librarys for that( that is kinda new for me sorry).

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

    Qt Code:
    1. bool ok;
    2.  
    3. QtJson::JsonObject result = QtJson::parse(myJson, ok).toMap();
    4. if(!ok) {
    5. qFatal("An error occurred during parsing");
    6. }else
    7. {
    8. qDebug() << "data" << result["data"].toString();
    9. qDebug() << " try1" << result["Name"].toString();
    10.  
    11. QtJson::JsonObject nested = result["data"].toMap();
    12. qDebug() << "try2:" << nested["Name"].toString();
    To copy to clipboard, switch view to plain text mode 

    But try1 and try2 qDebug() are empy.

    Thank you!
    Last edited by roseicollis; 9th February 2015 at 16:46.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Parsing a JSon webservice reply issue

    Try1 is empty for the same reason as your original post. There is no "Name" key in the outer JSON object.

    Try2 is empty because you have not run the result["data"] string through the QtJson::parse() function to get at the object it encodes.

    Your input data is this:
    Qt Code:
    1. {
    2. "data": "[{\"Name\":\"One\",\"Code\":\"\"},{\"Name\":\"Two\",\"Code\":\"Alpha\"}]"
    3. }
    To copy to clipboard, switch view to plain text mode 
    That is, your "data" string is a JSON encoded message (string) inside another JSON object.
    And your code is expecting this:
    Qt Code:
    1. {
    2. "data": [
    3. {
    4. "Name": "One",
    5. "Code": ""
    6. },
    7. {
    8. "Name": "Two",
    9. "Code": "Alpha"
    10. }
    11. ]
    12. }
    To copy to clipboard, switch view to plain text mode 
    . Which is, as wysota pointed out, what you would more typically expect. The situation is odd but not unworkable.

  7. #7
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing a JSon webservice reply issue

    Oh I see...I've tried this :

    Qt Code:
    1. QtJson::JsonObject result = QtJson::parse(myJson, ok).toMap();
    2. QtJson::JsonObject bbb = QtJson::parse(result["data"].toString(), ok).toMap();
    3. qDebug() << bbb;
    To copy to clipboard, switch view to plain text mode 
    But it only prints: QMap()

    And I've tried too:
    Qt Code:
    1. QtJson::JsonArray nested = result["data"].toMap();
    2. qDebug() << "try2:" << nested["Name"].toString();
    To copy to clipboard, switch view to plain text mode 
    Which says:
    Qt Code:
    1. conversion from 'QMap<QString, QVariant>' to non-scalar type 'QtJson::JsonArray {aka QList<QVariant>}' requested
    To copy to clipboard, switch view to plain text mode 

    I'm really stuck there, trying to parse this simple JSon... -.-'' Could you please put a code-example or explain it in other way? I have a really mess on my head right now trying to understand all this with Qmaps, QVariants and lists...I'm using this library: JSon

    Thank you and sorry for the problems.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing a JSon webservice reply issue

    Your data value is an array, not a map. It is an array containing two maps (objects) to be exact.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing a JSon webservice reply issue

    Finally I understood it... more or less... And I have this:

    Qt Code:
    1. QtJson::JsonObject outerObject = QtJson::parse(myJson, ok).toMap();
    2. QString innerString = outerObject["data"].toString();
    3. QtJson::JsonArray outerArray = QtJson::parse(innerString, ok).toList();
    4. auto object1 = outerArray[0].toMap();
    5. qDebug() << " DD" << object1["Name"];
    To copy to clipboard, switch view to plain text mode 

    which output is: DD QVariant(QString, “One”)

    But I can't get only de "One" string because I don't really know what is that output... does it represent to be a list of one QVariant which have a QString or... I don't know...

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing a JSon webservice reply issue

    Quote Originally Posted by roseicollis View Post
    Finally I understood it... more or less... And I have this:

    Qt Code:
    1. QtJson::JsonObject outerObject = QtJson::parse(myJson, ok).toMap();
    2. QString innerString = outerObject["data"].toString();
    3. QtJson::JsonArray outerArray = QtJson::parse(innerString, ok).toList();
    4. auto object1 = outerArray[0].toMap();
    5. qDebug() << " DD" << object1["Name"];
    To copy to clipboard, switch view to plain text mode 

    which output is: DD QVariant(QString, “One”)

    But I can't get only de "One" string because I don't really know what is that output... does it represent to be a list of one QVariant which have a QString or... I don't know...
    I have no idea what you mean. Have a look at QVariant api how to determine if the variant is a string, int, llis or map.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Parsing a JSon webservice reply issue

    The value object1["Name"] is returned as a QVariant and needs to be converted to a base type using toString(), toInt() etc. it has to be done this way because the Javascript value can be anything including another array or object.

  12. #12
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing a JSon webservice reply issue

    Quote Originally Posted by ChrisW67 View Post
    The value object1["Name"] is returned as a QVariant and needs to be converted to a base type using toString(), toInt() etc. it has to be done this way because the Javascript value can be anything including another array or object.
    This is what I thought but I can't put anything after ] and if i force it with object1["Name"].toString it says that there is nothing like toString()...

    Edit: Now it works dunno why so... problem solved.

    Thank you for all guys!
    Last edited by roseicollis; 11th February 2015 at 08:43.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing a JSon webservice reply issue

    After ] try putting a dot (.) followed by "type()" and see what that method returns.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. json parsing
    By sabbu in forum Newbie
    Replies: 1
    Last Post: 17th June 2011, 13:04
  2. Replies: 2
    Last Post: 21st January 2011, 11:50
  3. JSON - parsing (arrays)
    By Tomasz in forum Newbie
    Replies: 2
    Last Post: 29th December 2010, 13:08
  4. parsing JSON format
    By Raajesh in forum Qt Programming
    Replies: 5
    Last Post: 11th September 2010, 23:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.