PDA

View Full Version : How to parse Json array give below



Radhika
30th November 2015, 09:33
{
"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();

d_stranz
30th November 2015, 16:06
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.

Qiieha
30th November 2015, 21:49
Pass QJsonParseError to the fromJson-method. After fixing your json, you'll be able to parse your json.

Radhika
1st December 2015, 09:35
{
"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

ChrisW67
1st December 2015, 10:02
Yes, we understand that. However, until you present the JSON parser with valid JSON (http://json.org/) you will continue to have trouble:


{
"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",
}
}
]
}



Line 12. Error:Invalid comma, expecting }
Line 13. Error:Expecting string, not }

Radhika
1st December 2015, 10:56
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

Vikram.Saralaya
1st December 2015, 12:24
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
QJsonValue value = jsonObject.value("enclosures");
QJsonArray array = value.toArray();

This code should work fine.


qDebug() << array; gives the output as expected:

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 "}}])

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:

foreach (const QJsonValue & val, array)
{
QJsonObject dataObj = val.toObject().value("data").toObject();
qDebug() << dataObj.value("enclosure_id").toString();
}

PS: If you want to store enclosure_id as integer remove the double quotes.

Radhika
2nd December 2015, 06:01
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::onAuthenticationRequestSlot(QNetworkRe ply *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

Vikram.Saralaya
2nd December 2015, 09:20
That doesn't seem like a related problem. Once you receive the QJsonDocument the solution mentioned above should work.