-1

I want to fetch multiple JSONArray from an JSONObject

JSONObject jsonObject=new JSONObject(response_view); 
for(int x=0; x<jsonObject.length(); x++) {  
 JSONArray jsonArray = jsonObject.getJSONArray("Here I have to pass");
}

This is my JSON,

{
    "Mr. VICKRAM SINGH_269": [{
        "status": "P",
        "date": "2017-02-05"
    }, {
        "status": "P",
        "date": "2017-02-06"
    }],
    "Mr. VIVEK KUMAR YADAV_276": [{
        "status": "P",
        "date": "2017-02-05"
    }, {
        "status": "P",
        "date": "2017-02-06"
    }]
}
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33

2 Answers2

1

you can parse Json like below:

try{
    JSONObject json = new JSONObject(jsonRespondeString);
    Iterator<String> iterator =  json.keys();
    while (iterator.hasNext()){
           String key =  iterator.next();
           JSONArray jsonArray = json.getJSONArray(key);
           for(int i=0;i<jsonArray.length();i++){
               JSONObject object = jsonArray.getJSONObject(i);
               String value1 = object.getString("key1");
               String value2 = object.getString("key2");
            }
        }
    }
 catch (JSONException e){
      e.printStackTrace();
}

you can get name and id from key like this:

String str="Mr. VICKRAM SINGH_269";
String array[]=  str.split("_");
String name =array[0];
String id =array[1];
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0
JSONArray jsonArray=jsonObject.getJSONArray("Write the name of your array here");

and then loop your jsonArray Object

anzaidemirzoi
  • 386
  • 4
  • 13
  • array is dynamic...I dont know the name of an array – Rishabh Saxena Mar 01 '17 at 12:35
  • post your JSON @RishabhSaxena – Zaki Pathan Mar 01 '17 at 12:37
  • {"key":["item","item2","item3"]} you should have a key for your array in json string, as it is 'key' here. otherwise, your json is not a valid json. Could you please post your json string? – anzaidemirzoi Mar 01 '17 at 12:38
  • {"Mr. VICKRAM SINGH_269":[{"status":"P","date":"2017-02-05"},{"status":"P"‌​,"date":"2017-02-06"‌​}],"Mr. VIVEK KUMAR YADAV_276":[{"status":"P","date":"2017-02-05"},{"status":"P"‌​,"date":"2017-02-06"‌​}]} Array is Dynamic so we cannot fetch using name of it – Rishabh Saxena Mar 01 '17 at 12:52