Json - access data in multidimensional arrays
-
Hello!
I try to access some data from a search result in Json. For example.
[ { "score": 2.899999, "site": { "id": 1, "url": "http://www.google.com", "name": "Google.com", "type": "search", "language": "english", "category": [ "search engine" ], "status": "online", "refresh": 30, "lastcheck": "2015-12-17", "schedule": { "time": "20:00", "days": [ "sunday" ] } } } ]
My code:
QJsonDocument search = QJsonDocument::fromJson(myJson.toUtf8()); QJsonObject getjson = search.object(); QJsonValue arrayValue = getjson.value("site"); QJsonArray jArray = arrayValue.toArray(); qDebug() << jArray.count(); foreach(const QJsonValue & v, jArray) { double vDouble = v.toObject().value("id").toDouble(); QString my_ID = QString::number(vDouble); QString my_NAME = v.toObject().value("name").toString(); qDebug() << "my_ID" << my_ID; qDebug() << "my_NAME" << my_NAME; }
But if the Json looks like this:
{ "score": 2.899999, "site": { "id": 1, "url": "http://www.google.com", "name": "Google.com", "type": "search", "language": "english", "category": [ "search engine" ], "status": "online", "refresh": 30, "lastcheck": "2015-12-17", "schedule": { "time": "20:00", "days": [ "sunday" ] } } }
Without [ and ] at the beginning an end, i can access the data. After a lot of try and error, i can't find the right solution to overcome the [ ]. Anyone knows how?
Thanks!
-
Hi,
Because site is an object and not an array.
QJsonValue siteValue = getjson.value("site"); QJsonObject siteObject = siteValue.toObject(); qDebug() << siteObject.value("id");
-
Yes, but it seems to be an empty object. In jq i would access it like [] .id but
QJsonValue siteValue = getjson.value("site"); QJsonObject siteObject = siteValue.toObject(); qDebug() << siteObject.value("id");
only returns
QJsonValue(undefined)
How can i access data inside an undefined object? I could access it like above if the jsons looks like this
[ "test": { "site": { "id" } } ]
-
OK... so, ... well ok, It is not working in Qt. I can access all the data if i command line it through jq (https://stedolan.github.io/jq/) but not in Qt directly.
Your example returns
[ { "score": 2.899999, "site": { "id": 1, "url": "http://www.google.com", "name": "Google.com", "type": "search", "language": "english", "category": [ "search engine" ], "status": "online", "refresh": 30, "lastcheck": "2015-12-17", "schedule": { "time": "20:00", "days": [ "sunday" ] } } } QJsonValue siteValue = getjson.value("site"); QJsonObject siteObject = siteValue.toObject(); qDebug() << siteObject.value("id"); QJsonValue(undefined)
Why?
-
@qDebug said:
Without [ and ] at the beginning an end, i can access the data. After a lot of try and error, i can't find the right solution to overcome the [ ].
[ ]
represents an array.{ }
represents an object.
NOTE: Your document does not contain any multidimensional arrays! It does contain objects-inside-arrays, arrays-inside-objects, and objects-inside-objects.
QJsonDocument search = QJsonDocument::fromJson(myJson.toUtf8()); QJsonObject getjson = search.object();
...
QJsonValue(undefined)
Why?
Look closely at your JSON document structure.
Your top-level structure is a JSON array, so you must convert the document to a
QJsonArray
, not aQJsonObject
.Then, notice that your top-level array contains 1 object. Extract this object from the array first. Then, you can access the object's "site" field (which is also an object).