How to access this JSON ( indented JSON )



  • 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 :/



  • 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
    }
    
    


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


  • Moderators

    @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



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


  • Lifetime Qt Champion

    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.



  • 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);
        
    
    
        
    
    
       
    
    
    

  • Lifetime Qt Champion

    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.



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


Log in to reply
 

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