Results 1 to 9 of 9

Thread: How to parse Json array give below

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to parse Json array give below

    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonObject jsonObject = jsonResponse.object();
    QJsonValue value = jsonObject.value("enclosures");
    QJsonArray array = value.toArray();

    This code should work fine.

    Qt Code:
    1. qDebug() << array;
    To copy to clipboard, switch view to plain text mode 
    gives the output as expected:
    Qt Code:
    1. QJsonArray([{"data":{"enclosure_id":"0","enclosure_model":"MD1220 ","no_of_fans":"4","no_of_power_supplies":"2","no_of_slots":"24","product_revision_level":"1.01","temp_sensor_count":"4","vendor":"DELL "}}])
    To copy to clipboard, switch view to plain text mode 

    Make sure your file/json is not corrupt. You can easily do that by adding a qDebug() statement after each line and see what you have then..

    Edit:
    Apparently I see there is a problem with how you read from this array in your first post:

    Working version:
    Qt Code:
    1. foreach (const QJsonValue & val, array)
    2. {
    3. QJsonObject dataObj = val.toObject().value("data").toObject();
    4. qDebug() << dataObj.value("enclosure_id").toString();
    5. }
    To copy to clipboard, switch view to plain text mode 

    PS: If you want to store enclosure_id as integer remove the double quotes.
    Last edited by Vikram.Saralaya; 1st December 2015 at 12:43.

Similar Threads

  1. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  2. what should I give url?
    By sups in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2011, 23:22
  3. Replies: 2
    Last Post: 12th November 2010, 14:42
  4. how to parse json with QScriptEngine
    By DavidOrange in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2010, 11:22
  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

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
  •  
Qt is a trademark of The Qt Company.