Results 1 to 3 of 3

Thread: JSON - parsing (arrays)

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default JSON - parsing (arrays)

    Hello!

    I'm trying to parse JSON. Simple JSON message is no problem, but I don't know how can I parse arrays. My code:

    JSON data:
    Qt Code:
    1. newData = {
    2. "sucess":true,
    3. "msg":"Success!",
    4. "count":2,
    5. "data":[
    6. {
    7. "id":"1",
    8. "title":"first",
    9. "type":"request",
    10. "created_at":"2010-12-20 09:00:00",
    11. "sender_id":"1",
    12. "sender_name":"someone"
    13. },
    14. {
    15. "id":"2",
    16. "title":"second",
    17. "type":"notification",
    18. "created_at":"2010-12-21 09:00:00",
    19. "sender_id":"1",
    20. "sender_name":"someone"
    21. }
    22. ]
    23. }";
    To copy to clipboard, switch view to plain text mode 

    Parsing code:
    Qt Code:
    1. qDebug() << newData << endl;
    2.  
    3. QScriptValue sc;
    4. QScriptEngine engine;
    5. sc = engine.evaluate("(" + QString(newData) + ")");
    6.  
    7. if (sc.property("sucess").toBool())
    8. {
    9. qDebug() << sc.property("msg").toString() << endl;
    10. qDebug() << sc.property("count").toInteger() << endl;
    11.  
    12. qDebug() << endl << sc.property("data").toString() << endl;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Last line, when I want to parse 'data' array gives:
    Qt Code:
    1. "[object Object],[object Object]"
    To copy to clipboard, switch view to plain text mode 

    How can I parse that piece of message? I don't want to use QJSON.

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

  3. The following user says thank you to squidge for this useful post:

    Tomasz (26th December 2010)

  4. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: JSON - parsing (arrays)

    Everything works. But what should I do when 'data' isn't an array but just an object:

    {
    "success":true,
    "msg":"success",
    "count":1,
    "data":
    {
    "id":"0",
    "title":"title",
    "content":"message",
    "type":"note",
    "created_at":"2010-12-29 09:30:00",
    "sender_id":"1",
    "sender_name":"someone"
    }
    }

    Should I use QScriptValueIterator too? I'm trying to use it but with no result.

    thanks in advance
    best regards
    Tomasz

Similar Threads

  1. how to parse json with QScriptEngine
    By DavidOrange in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2010, 11:22
  2. parsing JSON format
    By Raajesh in forum Qt Programming
    Replies: 5
    Last Post: 11th September 2010, 23:40
  3. Qt and JSON
    By Chiggins in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2010, 13:12
  4. Build json, how to?
    By martinn in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2010, 15:19
  5. json: eval
    By mickey in forum General Programming
    Replies: 3
    Last Post: 17th January 2008, 19:50

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.