Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Json - access data in multidimensional arrays
QtWS25 Last Chance

Json - access data in multidimensional arrays

Scheduled Pinned Locked Moved Solved General and Desktop
qjsondocumentqjsonobjectqjsonvalueqjsonarray
9 Posts 4 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qDebug
    wrote on 17 Feb 2016, 20:55 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Feb 2016, 22:37 last edited by
      #2

      Hi,

      Because site is an object and not an array.

      QJsonValue siteValue = getjson.value("site");
      QJsonObject siteObject = siteValue.toObject();
      qDebug() << siteObject.value("id");
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qDebug
        wrote on 17 Feb 2016, 22:55 last edited by
        #3

        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" } } ]
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Feb 2016, 23:34 last edited by
          #4

          @qDebug said:

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

          That's not a valid json structure

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply 17 Feb 2016, 23:39
          0
          • S SGaist
            17 Feb 2016, 23:34

            @qDebug said:

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

            That's not a valid json structure

            Q Offline
            Q Offline
            qDebug
            wrote on 17 Feb 2016, 23:39 last edited by
            #5

            @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?

            T J 2 Replies Last reply 18 Feb 2016, 00:17
            0
            • Q qDebug
              17 Feb 2016, 23:39

              @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?

              T Offline
              T Offline
              Tyras
              wrote on 18 Feb 2016, 00:17 last edited by
              #6

              @qDebug
              Maybe because that '[' in JSON's first line should be '{' ?

              When a coder says that it's impossible to do something, he's actually feeling too lazy to do it.

              T 1 Reply Last reply 18 Feb 2016, 00:19
              0
              • T Tyras
                18 Feb 2016, 00:17

                @qDebug
                Maybe because that '[' in JSON's first line should be '{' ?

                T Offline
                T Offline
                Tyras
                wrote on 18 Feb 2016, 00:19 last edited by
                #7

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

                When a coder says that it's impossible to do something, he's actually feeling too lazy to do it.

                1 Reply Last reply
                0
                • Q qDebug
                  17 Feb 2016, 23:39

                  @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?

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 18 Feb 2016, 00:36 last edited by JKSH
                  #8

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

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  Q 1 Reply Last reply 18 Feb 2016, 01:20
                  4
                  • J JKSH
                    18 Feb 2016, 00:36

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

                    Q Offline
                    Q Offline
                    qDebug
                    wrote on 18 Feb 2016, 01:20 last edited by
                    #9

                    @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!

                    1 Reply Last reply
                    0

                    9/9

                    18 Feb 2016, 01:20

                    • Login

                    • Login or register to search.
                    9 out of 9
                    • First post
                      9/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved