Question

I'm trying to get json value form api. There is one object key that is dynamic so how can I solve it?

I got json data as like in the picture.

enter image description here there is the numbers after "pages" that numbers are randomly changing and so how can I get that object value?

Was it helpful?

Solution

Do current JSON String Parsing as:

   JSONObject jObject = new JSONObject("your json String");
   JSONObject jObjquery = jObject.getJSONObject("query"); // get query JSONObject
   JSONObject jObjpages = jObjquery.getJSONObject("pages");//get pages JSONObject
   // get dynamic keys from pages object
            Iterator<?> keys = jObjpages.keys(); 

     while( keys.hasNext() ){
        String key = (String)keys.next();
        if( jObjpages.get(key) instanceof JSONObject ){
           // get all values from JSONObject
           String str_pageid=jObjpages.optString("pageid"); 
         //get  ns, title, extract,.. in same way from jObjpages 
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top