Results 1 to 6 of 6

Thread: How to parse nested JSON Array and Object inside array

  1. #1
    Join Date
    Nov 2015
    Location
    India
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question How to parse nested JSON Array and Object inside array

    Qt Code:
    1. {
    2. "physicaldrives":
    3. [
    4. {
    5. "data":
    6. {
    7. "dev_id": "0",
    8. "ssd_life_left": "0"
    9. }
    10. },
    11. {
    12. "data":
    13. {
    14. "dev_id": "1",
    15. "ssd_life_left": "10"
    16.  
    17. }
    18. }
    19. ]
    20. }
    To copy to clipboard, switch view to plain text mode 


    this is snippet of JSON how can I parse it in Qt 5

    i have tried this
    Qt Code:
    1. QString strReply = (QString)reply->readAll();
    2.  
    3. QStringList phydata;
    4.  
    5. QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    6. if(jsonResponse.isEmpty())
    7. return false;
    8.  
    9. QJsonObject jsonObj = jsonResponse.object();
    10. QJsonArray jsonArray = jsonObj["physicaldrives"].toArray();
    11. if(jsonArray.isEmpty())
    12. return false;
    13. qDebug()<< "jsonArray phyDatas" << jsonArray ;
    14.  
    15. // fatching JSONArray of Ph
    16. foreach (const QJsonValue & value, jsonArray)
    17. {
    18. QJsonObject obj = value.toObject();
    19. phydata.append(obj["data"].toString());
    20. qDebug()<< "phyDatas" << phydata ;
    21. }
    22.  
    23. // fatching value from JSONArray
    24. QJsonValue val = obj["dev_id"];
    25.  
    26. qDebug()<< "devID" << val.toInt(); ;
    To copy to clipboard, switch view to plain text mode 
    but I dont know its right way or not?
    how can I get list of "drv_id" of "data" object ??????

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to parse nested JSON Array and Object inside array

    This is not likely the actual code, because it won't compile as obj in line 24 is an unknown identifier.

    Get the object for "data" and then access that object's "dev_id" property.

    Btw, unnecessary double conversion QByteArray -> QString -> QByteArray right at the beginning.

    Cheers,
    _

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

    Radhika (16th December 2015)

  4. #3
    Join Date
    Nov 2015
    Location
    India
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to parse nested JSON Array and Object inside array

    Will you explain it with code?


    I have tried this but not working

    Qt Code:
    1. // fetching JSONArray
    2. foreach (const QJsonValue & value, jsonArray)
    3.  
    4. {
    5. QJsonObject jobj = value.toObject();
    6. QJsonValue val = jobj.value(QString("data"));
    7. qDebug() << "data" <<val.toString() << endl;
    8.  
    9. QJsonObject item = val.toObject();
    10. QJsonValue subobj = item["dev_id"];
    11. QString str = subobj.toString();
    12. qDebug() << "dev_id" <<str << endl;
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Radhika; 16th December 2015 at 08:48.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to parse nested JSON Array and Object inside array

    That looks about right, though I wonder why you use value() when getting "data" but the index operator when getting other fields
    Which debug output does not display what you expect? I.e. where does it start to go wrong?

    Cheers,
    _

  6. #5
    Join Date
    Nov 2015
    Location
    India
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to parse nested JSON Array and Object inside array

    I am not getting you
    kindly explain it with code

    Qt Code:
    1. if(jsonArray.isEmpty())
    2. return false;
    3. qDebug()<< "jsonArray phyDatas" << jsonArray ;
    To copy to clipboard, switch view to plain text mode 


    till this its working fine but after dat while getting data and dev_id I am not getting dev_id
    i got "" blank response

    will you plz explain it with correct code??
    Last edited by Radhika; 17th December 2015 at 05:07.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to parse nested JSON Array and Object inside array

    Attach a buildable and runnable test program.

    Cheers,
    _

Similar Threads

  1. How to parse Json array give below
    By Radhika in forum Qt Programming
    Replies: 8
    Last Post: 2nd December 2015, 09:20
  2. array of typedef struct declared inside class
    By Ryuuji in forum General Programming
    Replies: 4
    Last Post: 22nd November 2012, 07:00
  3. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  4. Replies: 2
    Last Post: 12th November 2010, 14:42
  5. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24

Tags for this Thread

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.