Results 1 to 9 of 9

Thread: How to parse Json array give below

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

    Default How to parse Json array give below

    {
    "enclosures":
    [
    {
    "data":
    {
    "enclosure_id": "0",
    "vendor": "DELL ",
    "enclosure_model": "MD1220 ",

    "power_status":
    [
    "0",
    "3"
    ],
    "temp_status":
    [
    "1",
    "1",
    "0",
    "1"
    ],
    "temperatures":
    [
    "-20",
    "-20",
    "186",
    "180"
    ],
    "fan_status":
    [
    "1",
    "0",
    "1",
    "1"
    ],

    "alarm_count": "1",
    "drive_ids":
    [
    "2",
    "10",
    ],
    "physicaldrives":
    [
    {
    "data":
    {
    "dev_id": "0",
    "ssd_life_left": "0",
    "temperature": "28",
    "usable_capacity": "286749479",

    }
    },
    I want to access the value of "enclosure_id" inside data object which is inside enclosure how can I do that ? I am trying this code but not getting value
    //parse json
    //qDebug() << "enclosures:" << strReply;
    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());

    QJsonObject jsonObj = jsonResponse.object();
    QJsonValue value = jsonObj.value("enclosures");
    QJsonArray array = value.toArray();
    foreach (const QJsonValue & val, array)
    qDebug() << val.toObject().value("enclosure_id").toInt();

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to parse Json array give below

    Aside from a couple of qDebug() statements, you do no error checking. How do you know that any of the objects you are creating are valid?

    In the JSON code you posted, I count 5 open braces "{" and only two close braces "}", as well as an unmatched open bracket "[". If that's the entire JSON string, then I doubt that you are creating a valid document from it.

  3. #3
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse Json array give below

    Pass QJsonParseError to the fromJson-method. After fixing your json, you'll be able to parse your json.

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

    Default Re: How to parse Json array give below

    {
    "enclosures":
    [
    {
    "data":
    {
    "enclosure_id": "0",
    "vendor": "DELL ",
    "enclosure_model": "MD1220 ",
    "no_of_slots": "24",
    "product_revision_level": "1.01",
    "no_of_fans": "4",
    "no_of_power_supplies": "2",
    "temp_sensor_count": "4"
    }
    }
    ]
    }


    I am trying to parse "enclosure ID" inside that "data" object Want to access "enclosure" array
    Last edited by Radhika; 1st December 2015 at 10:54.

  5. #5
    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: How to parse Json array give below

    Yes, we understand that. However, until you present the JSON parser with valid JSON you will continue to have trouble:
    Qt Code:
    1. {
    2. "enclosures":[
    3. {
    4. "data":{
    5. "enclosure_id":"0",
    6. "vendor":"DELL ",
    7. "enclosure_model":"MD1220 ",
    8. "no_of_slots":"24",
    9. "product_revision_level":"1.01",
    10. "no_of_fans":"4",
    11. "no_of_power_supplies":"2",
    12. "temp_sensor_count":"4",
    13. }
    14. }
    15. ]
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Line 12. Error:Invalid comma, expecting }
    2. Line 13. Error:Expecting string, not }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to parse Json array give below

    ya ,structure is valid , it's just snippet of whole JSON file, Just wanna know how to access enclosure array
    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonObject jsonObject = jsonResponse.object();

    QJsonValue value = jsonObject.value("enclosures");
    QJsonArray array = value.toArray();


    I have tried this but it is not working

  7. #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.

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

    Default Re: How to parse Json array give below

    error "Host requires authentication"
    URL Reply: "{\n \"self\":\n {\n \"uri\": \"/kirk/1.0/hosts/0/enclosures\",\n \"method\": \"GET\"\n },\n \"status\":\n {\n \"code\": \"100\",\n \"error\": \"NOT_AUTHORIZED\",\n \"detail\": \"2\",\n \"description\": \"[session-id empty] \"\n }\n}\n"

    I am getting this error

    I have tried this
    void MainWindow:nAuthenticationRequestSlot(QNetworkReply *reply, QAuthenticator *aAuthenticator)
    {
    qDebug() << "INSIDE ";
    aAuthenticator->setUser("root");
    aAuthenticator->setPassword("a@123");
    }

    QObject::connect(&mgr, SIGNAL(authenticationRequired(QNetworkReply*,QAuth enticator*)), SLOT(onAuthenticationRequestSlot(QNetworkReply*,QA uthenticator*)) );

    but not working it seems

  9. #9
    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

    That doesn't seem like a related problem. Once you receive the QJsonDocument the solution mentioned above should work.

  10. The following user says thank you to Vikram.Saralaya for this useful post:

    Radhika (15th December 2015)

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.