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. How to access this JSON ( indented JSON )
Forum Updated to NodeBB v4.3 + New Features

How to access this JSON ( indented JSON )

Scheduled Pinned Locked Moved Solved General and Desktop
json
9 Posts 4 Posters 2.8k Views 3 Watching
  • 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.
  • QjayQ Offline
    QjayQ Offline
    Qjay
    wrote on last edited by
    #1

    Hey , guys . Please help me on how to access this json elements ?
    JSON :

    {
    	"batchcomplete": "",
    	"query": {
    		"searchinfo": {
    			"totalhits": 1
    		},
    		"search": [{
    			"ns": 0,
    			"title": "Intro cryptography",
    			"snippet": "Introduction to <span class='searchmatch'>Cryptography</span>  \n'''What is <span class='searchmatch'>Cryptography</span> ?'''\n",
    			"size": 1815,
    			"wordcount": 243,
    			"timestamp": "2016-03-07T15:34:27Z"
    		}]
    	}
    }
    

    Elements that i want to access is : title , totalhits and snippet .
    Here is what i have tried :
    query.search["title"] but it was no good :/

    1 Reply Last reply
    0
    • mistralegnaM Offline
      mistralegnaM Offline
      mistralegna
      wrote on last edited by
      #2

      Hello,

      Assuming you have a QJsonDocument named doc describing the above json, you have to call :

      QJsonDocument doc = QJsonDocument::fromJson(content); // content is the json to parse
      QJsonObject root = doc.rootObject();
      int totalHints = root["query"].toObject()["searchinfo"].toObject()["totalhits"].toInt();
      

      For the elements title and snippet inside your search array, you can iterate over the array (root["query"].toObject()["search"].toArray()) and convert each item to a QJsonObject currentItem to access currentItem["title"]

      Something like:

      QJsonArray allSearches = root["query"].toObject()["search"].toArray();
      
      // C++11
      
      for(const auto& item : allSearches)
      {
        QJsonObject currentItem = item.toObject();
        QString title = currentItem["title"].toString(); // Idem for snippet
      }
      
      
      1 Reply Last reply
      4
      • QjayQ Offline
        QjayQ Offline
        Qjay
        wrote on last edited by Qjay
        #3

        Hey @mistralegna thanks for the reply . I was actually looking for a way to do it in js. Can you give an example for js?

        JKSHJ 1 Reply Last reply
        0
        • QjayQ Qjay

          Hey @mistralegna thanks for the reply . I was actually looking for a way to do it in js. Can you give an example for js?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Qjay said:

          I was actually looking for a way to do it in js. Can you give an example for js?

          http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object

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

          1 Reply Last reply
          1
          • QjayQ Offline
            QjayQ Offline
            Qjay
            wrote on last edited by
            #5

            I know how to parse json , i want to access the elements in it after parsing it .

            for example i have this JSON :

            https://ghostbin.com/paste/j93oe

            and i access it like this

             parse = JSON.parse(json);
                            rev_id = parse.parse.revid; // gor revid
                            console.log(rev_id);
            
                            text = parse.parse.text["*"]; // got text
            

            but i don't know how to access the type in my question

            if possible can you write it here ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              From your original post, the search field is a list so you have to handle like one.

              If you only have one element to parse:

              query.search[0].title
              

              otherwise use a for loop and go through it.

              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
              • QjayQ Offline
                QjayQ Offline
                Qjay
                wrote on last edited by
                #7

                Hey @SGaist I tried this simple js code

                and it's giving me undefined in alert box .

                    
                        var http = new XMLHttpRequest();
                        var json , parse , title ;
                
                        
                
                        http.onreadystatechange = function(){
                            if(http.readyState == 4 && http.status == 200)
                            { json = http.responseText;
                
                                parse = JSON.parse(json);
                                title = query.search[0].title;
                                
                            }
                        };
                        http.open('GET',"http://en.wikitolearn.org/api.php?action=query&list=search&srsearch=cryptography&format=json");
                        http.send();
                
                
                        alert(title);
                    
                
                
                    
                
                
                   
                
                
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You put the result of JSON.parse in a variable called parse and then you try to get your data from a variable called query.

                  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
                  1
                  • QjayQ Offline
                    QjayQ Offline
                    Qjay
                    wrote on last edited by
                    #9

                    Crap i mixed 2 codes. Sorry for that :m. Thank you everyone

                    1 Reply Last reply
                    1

                    • Login

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