[HEHLP] Get values from object inside object in a Json file
-
Hello, I have a Json file like this :
{"country":"Morocco", "province":["mainland"], "timeline":{"cases":{"5/24/20":7433, "5/25/20":7532, "5/26/20":7577, "6/22/20":10172}, "deaths":{"5/24/20":199, "5/25/20":200, "5/26/20":202, "6/22/20":214}, "recovered":{"5/24/20":4703, "5/25/20":4774, "5/26/20":4881, "6/22/20":8366}}}
And I want to store the values that are in front of the dates in a Vector; for example I have | "6/22/20":8366 | I want to store 8366 in the vector. I'm really not good with the parsing of the Json files. This is what I did but didn't work, what have I done wrong or what is the correct way to do it?
QJsonObject j_obj = json_doc.object(); QJsonValue val, inner_val; QVector<double> y(7); for(auto jsonObj : j_obj) { val = jsonObj.toObject().value("cases"); if(!val.isUndefined()) { for (auto jsobj : jsonObj.toObject()) { inner_val = jsobj.toObject().value(""); y[i] = inner_val.toInt(); qDebug() << y[i]; i++; } } val = 0; }
-
Hello, I have a Json file like this :
{"country":"Morocco", "province":["mainland"], "timeline":{"cases":{"5/24/20":7433, "5/25/20":7532, "5/26/20":7577, "6/22/20":10172}, "deaths":{"5/24/20":199, "5/25/20":200, "5/26/20":202, "6/22/20":214}, "recovered":{"5/24/20":4703, "5/25/20":4774, "5/26/20":4881, "6/22/20":8366}}}
And I want to store the values that are in front of the dates in a Vector; for example I have | "6/22/20":8366 | I want to store 8366 in the vector. I'm really not good with the parsing of the Json files. This is what I did but didn't work, what have I done wrong or what is the correct way to do it?
QJsonObject j_obj = json_doc.object(); QJsonValue val, inner_val; QVector<double> y(7); for(auto jsonObj : j_obj) { val = jsonObj.toObject().value("cases"); if(!val.isUndefined()) { for (auto jsobj : jsonObj.toObject()) { inner_val = jsobj.toObject().value(""); y[i] = inner_val.toInt(); qDebug() << y[i]; i++; } } val = 0; }
@Elmehdi said in [HEHLP] Get values from object inside object in a Json file:
"cases"
cases is inside "timeline", so you first need to search for "timeline"...
-
@jsulm Hello!
you said I should search for the timeline then search for cases. Can you help me about that, this is what I did but doesn't seem to work
QJsonObject timeline = j_obj["timeline"].toObject(); for(auto jsonObj : timeline) { qDebug() << "aisaiisisaiasiasiass" << y[i]; QJsonObject cases = timeline["cases"].toObject(); for (auto jsobj : cases) { inner_val = jsobj.toObject().value("5/24/20"); y[i] = inner_val.toInt(); qDebug() << "GOOOOOOOOOOOODD" << y[i]; i++; } }
-
@jsulm Hello!
you said I should search for the timeline then search for cases. Can you help me about that, this is what I did but doesn't seem to work
QJsonObject timeline = j_obj["timeline"].toObject(); for(auto jsonObj : timeline) { qDebug() << "aisaiisisaiasiasiass" << y[i]; QJsonObject cases = timeline["cases"].toObject(); for (auto jsobj : cases) { inner_val = jsobj.toObject().value("5/24/20"); y[i] = inner_val.toInt(); qDebug() << "GOOOOOOOOOOOODD" << y[i]; i++; } }
@Elmehdi
It looks about right, except that obviously it would only ever match the one"5/24/20"
key.but doesn't seem to work
You should say what "doesn't seem to work" means. I see some
qDebug()
, and you could probably do with some more, you should say whether they are hit, what the output is, so that we can match to the input. -
@JonB Sorry I don't understand you well,
- obviously it would only ever match the one "5/24/20" key
But it never prints the value of it.
Apparently it hit them all since the qDebug is called exactly 4 times the number of the dates but it didn't print the value.
- obviously it would only ever match the one "5/24/20" key