0

I have a strange json string that i receive from rest api, now I am trying to extract info from json string which looks like this.

var json={"student":{
"0":[{
    "name":"manet",
    "marks":114
}],
"1":null,
"2":null,
"4":null,
"5":null,
"6":null,
"7":[{
    "name":"Om",
    "marks":75
}], "employye": {
"0":[{
    "name":"nn",
    "value":23
}],
"1":[{"name": "tt",
      "value": 67}]
"2":null,
"3":null,
"4":null,
"5":null,
"6":null,
"7":[{
    "name":"Om",
    "value":75
}]

}};

I am trying to read this json like this but dont know how should I iterate over the value under student and employye

 try {
        JSONObject reader = new JSONObject(data);
            Log.d(TAG, ""+reader.length());
        JSONObject student = reader.getJSONObject("student");
                Log.d(TAG, "Student Array"+student.getJSONArray("0")); // here is the issue 

    } catch (JSONException j) {
        j.printStackTrace();

    }

Thanks

user2323
  • 49
  • 2
  • 11
  • possible duplicate of [How to parse this nested JSON array in android](http://stackoverflow.com/questions/17673057/how-to-parse-this-nested-json-array-in-android) – ben75 Oct 11 '14 at 20:12

1 Answers1

0

This user has the same query as yours. Just go through this answer.

https://stackoverflow.com/a/17673362/4132625

Use this code to get the keys under your student object

JSONObject object = new JSONObject(json);
JSONObject reader = object.getJSONObject("student");
Iterator<String> iterator = reader.keys();
while(iterator.hasNext()){
Log.d("JSON" ,"key :"+iterator.next());
}

which will give you all the keys under your student objects

key :0" key :1" key :2" key :4" key :5" key :6" key :7" key :employye"

and so on parse through your json.

Community
  • 1
  • 1
  • No but the issue is how shall I go through 1,2,3 which are String not int so I can not do this JSONObject elem = list.getJSONObject(i); instead I need to do soemthing like this JSONObject elem = list.getJSONObject("7"); – user2323 Oct 11 '14 at 15:56
  • use String.valueof() method to pass 7. – Shvet Oct 11 '14 at 16:09
  • You should use the "flag as duplicate" option rather than posting the link to another question as an answer. – brandonscript Oct 11 '14 at 16:17