3

I have a little issue with parsing JSON String which I get from a web server. So my JSON looks something like this :

{
 ..........
"statistics":{
              "660":{
                       "param_id":660,
                       "cat_id":2,
                       "param_title":"Number",
                       "param_value":"26",
                       "value_type":"singleline",
                       "elem_order":"1"}

            ,
              "662":{
                       "param_id":662,
                       "cat_id":2,
                       "param_title":"Name",
                       "param_value":"Dani",
                       "value_type":"singleline",
                       "elem_order":"2"
                    }
                    // --||--
           }
}

So I get a JSONObject statisics and I want to get JSONObjects from statistics, but the problem is that their name is different everytime.So I can't just do json.getJSONObject("660");. So any suggestions how can I do that?

Android-Droid
  • 14,365
  • 41
  • 114
  • 185
  • check my answer here http://stackoverflow.com/questions/7669730/java-representation-of-json-object/7669794#7669794 – Lalit Poptani Oct 15 '11 at 12:25
  • That looks good actually,but I can't say that my JSONObject names will start from 0 everytime. – Android-Droid Oct 15 '11 at 12:41
  • Well it will give you dynamically, its not important that your JSONObject should have to be 0 everytime, just try it. – Lalit Poptani Oct 15 '11 at 12:49
  • You just have to get "statistics" in a JSONObject and then a for loop upto jsonobj.length(). – Lalit Poptani Oct 15 '11 at 12:50
  • It's throwin me an exception that I don't have JSONObject with name 0. – Android-Droid Oct 15 '11 at 13:19
  • Can you show me your code and complete JSON???? you can use pastebin for that... – Lalit Poptani Oct 15 '11 at 13:21
  • So, here is the link of my JSON code : http://pastebin.com/rqsNGXBG . It's still just a piece of it, because LogCat can't store my whole JSON. And the code I'm using is here : http://pastebin.com/L8ZahTVX . So actually I almost fix that, but the problem which I have now is : I get only one JSONObject from "sttaistics". Example : I get only `660` like `stats.length()` times. Any suggestions? – Android-Droid Oct 15 '11 at 13:55

4 Answers4

9

You can do something like this :

if(jsonObj.has("statistics")){
   Iterator<Object> keys = stats.keys();
   while(keys.hasNext()){

     String key = (String) keys.next();
     JSONObject obj = new JSONObject();
     obj = stats.getJSONObject(key);
     // get JSON 

  }// end while

}//end if
Android-Droid
  • 14,365
  • 41
  • 114
  • 185
5

use JSONObject.keys() to get key iterator, then getJsonObject( key) to get object.

kingori
  • 2,406
  • 27
  • 30
1

use like this

JSONObject jsonOnb = json.getJSONObject("statistics") ;
JSONObject pagesObj = jsonOnb.getJSONObject(jsonOnb.names().getString(0));          

   Log.i("pageid", "pageid= " +pagesObj.get("param_id"));                  
   Log.i("title", "title= " +pagesObj.get("cat_id"));     
     ..............
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
1

Try below code-

JSONArray nameArray = statisics.names();
JSONArray valArray = statisics.toJSONArray(nameArray);

where names() method returns array of names and it is stored in nameArray and valArray contains array of values corresponding to valArray.

You can iterate using for loop over these array to get values.

anujprashar
  • 6,263
  • 7
  • 52
  • 86