chandeshwar Thakur chandeshwar Thakur - 3 months ago 11
Android Question

how to parse Json array that has above hundreads of arrays and its multiple objects

i am beginner at android can anybody tell me how to parse All son arrays that come under one fix array i want to parse all array name in parents and its object ,want to show in child view
because i am using Expendable List view ,so i am unable to add child in expendable list view .

{
"err-code":0,
"worksheet":[
{
"2016-08-19":[
{
"worksheet_id":130,
"job_no":"ES100353-AF",
"message":"Matthew Smith has submitted Worksheet for Approval",
"added_date":"19 Aug 2016",
"added_time":"06:31 AM",
"status":"Pending"
}
],
"2016-08-18":[
{
"worksheet_id":107,
"job_no":"ES100353-AF",
"message":"Matthew Smith has submitted Worksheet for Approval",
"added_date":"18 Aug 2016",
"added_time":"05:31 AM",
"status":"Pending"
},

]
}
}my code `private void getDataExpendable()
{
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
String url = "http://103.5.103.8:4067/services/Webservices/listing_worksheet_filled_by_engineers";
// getting data http://10.54.103.8:4067/evivaservices/Webservices/listing_worksheet_filled_by_engineers
HashMap<String, String> user = session.getUserDetails();
String token = user.get(SessionManager.KEY_TOKEN);
Log.e("token check", token);
final String role = user.get(SessionManager.KEY_ROLE);
Log.e("role", role);
String user_id = user.get(SessionManager.KEY_USERID);
Log.e("user_id", String.valueOf(user_id));
Map<String, String> params = new HashMap();
params.put(KEY_TOKEN, token);
params.put(KEY_ROLE, role);
params.put(KEY_ID, user_id);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
try {
String req = response.getString("err-code");
if (0 == Integer.parseInt(req)) {
Log.e("Data showing", response.toString());
parseDate(response);
}
else if(300==Integer.parseInt(req))
{
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
AlertDialog alertDialog=new AlertDialog.Builder(HomeScreenAdmin.this).create();
error.printStackTrace();
//TODO: handle failure
}
});
jsonRequest.setRetryPolicy(new DefaultRetryPolicy(
5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonRequest, "HomeScreenAdmin");
}

private void parseDate(JSONObject response)
{
try {
List<String> comingSoon = new ArrayList<String>();
JSONArray jsonArray = response.getJSONArray("worksheet");
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
Log.e("objects are", jsonObject.toString());
String key = null;
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
key = iterator.next();

Log.e("key are for under", key);
//here i am getting all array name
listDataHeader.add(key);
}
}
listDataChild.put(listDataHeader.get(0), comingSoon);

}
catch (JSONException e)
{

e.printStackTrace();
}
listAdapter1 = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter1);
}
}

Answer

i solved using another way `private void parseDate(JSONObject response) {

    try {
        JSONArray jsonArray = response.optJSONArray("worksheet");
        for (int i = 0; i <= jsonArray.length(); i++) {
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            Log.e("objects are", jsonObject.toString());
            String key = null;
            Iterator<String> iterator = jsonObject.keys();
            while (iterator.hasNext()) {
                key = iterator.next();
                ModelparentAdmin model=new ModelparentAdmin();
                model.setName(key);

               JSONArray js=jsonObject.getJSONArray(key);

                ch_list = new ArrayList<ModelChildAdmin>();
                for(int m=0;m<js.length();m++)
                {
                    JSONObject object=js.getJSONObject(m);
                    ModelChildAdmin ch = new ModelChildAdmin();
                    String message=object.getString("message");
                    String job_no=object.getString("job_no");
                    String added_date=object.getString("added_date");
                    String status=object.getString("status");
                    int worksheet_id=object.getInt("worksheet_id");
                    Log.e("key are for under", String.valueOf(worksheet_id));
                   ch.setJob_no(job_no);
                    ch.setAdded_date(added_date);
                    ch.setMessage(message);
                    ch.setStatus(status);
                    ch.setWorksheet_id(worksheet_id);
                    ch_list.add(ch);
                }
                model.setItems(ch_list);

                    list.add(model);
                Collections.sort(list, new Comparator<ModelparentAdmin>()
                {
                    public int compare(ModelparentAdmin o1, ModelparentAdmin o2)
                    {
                        if (o1.getName() == null || o2.getName() == null)
                            return 0;
                        return o2.getName().compareTo(o1.getName());
                    }
                });
            }
        }
    }
    catch (JSONException e)
    {
    e.printStackTrace();
    }


}`