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!


  • Lifetime Qt Champion

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

  • Lifetime Qt Champion

    @qDebug said:

    [ "test": { "site": { "id" } } ]

    That's not a valid json structure



  • @SGaist

    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
    Maybe because that '[' in JSON's first line should be '{' ?



  • Looking again, it shouldn't even exist...


  • Moderators

    @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 a QJsonObject.

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



  • @JKSH

    Thank you! That did clear my mind! :D

    QJsonDocument to array, then foreach, next object value "site" toObject and now tadaaa object.value("name").toString;

    Great, thanks again!



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.