[Solved] JSON parsing in android using inbuilt json library? [closed]


Try this way,hope this will help you to solve your problem.

try{
   String jsonRespone="{\"From\":\"13-06-2014\",\"To\":\"19-06-2014\",\"Employee\":[{\"EmpId\":\"1\",\"EmpCode\":\"101\",\"EmpName\":\"abc\",\"EmpLName\":\"def\",\"Job\":[{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"}]},{\"EmpId\":\"1\",\"EmpCode\":\"101\",\"EmpName\":\"abc\",\"EmpLName\":\"def\",\"Job\":[{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"},{\"ID\":\"1\",\"JobName\":\"abc\",\"Time\":\"12:00\"}]}]}";

   JSONObject responeJson = new JSONObject(jsonRespone);
   String From = responeJson.getString("From");
   String To = responeJson.getString("To");

   ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String, Object>>();

   JSONArray employeeJsonArray = responeJson.getJSONArray("Employee");
   for (int i=0;i<employeeJsonArray.length();i++){
        HashMap<String,Object> row = new HashMap<String, Object>();
        row.put("EmpId",employeeJsonArray.getJSONObject(i).getString("EmpId"));
        row.put("EmpCode",employeeJsonArray.getJSONObject(i).getString("EmpCode"));
        row.put("EmpName",employeeJsonArray.getJSONObject(i).getString("EmpName"));
        row.put("EmpLName",employeeJsonArray.getJSONObject(i).getString("EmpLName"));

        JSONArray jobJsonArray = employeeJsonArray.getJSONObject(i).getJSONArray("Job");
        ArrayList<HashMap<String,String>> jobList = new ArrayList<HashMap<String, String>>();
        for(int j=0; j<jobJsonArray.length(); j++){
            HashMap<String,String> job = new HashMap<String, String>();
            job.put("ID",jobJsonArray.getJSONObject(j).getString("ID"));
            job.put("JobName",jobJsonArray.getJSONObject(j).getString("JobName"));
            job.put("Time",jobJsonArray.getJSONObject(j).getString("Time"));
            jobList.add(job);
        }
        row.put("Job",jobList);
        data.add(row);
   }

   System.out.print("From : "+From);
   System.out.print("To : "+To);

   for (HashMap<String,Object> row :data){
       System.out.print("EmpId : "+row.get("EmpId").toString());
       System.out.print("EmpCode : "+row.get("EmpCode").toString());
       System.out.print("EmpName : "+row.get("EmpName").toString());
       System.out.print("EmpLName : "+row.get("EmpLName").toString());

       ArrayList<HashMap<String,String>> jobs = (ArrayList<HashMap<String,String>>) row.get("Job");
       for (HashMap<String,String> job :jobs){
           System.out.print("ID : "+job.get("ID"));
           System.out.print("JobName : "+job.get("JobName"));
           System.out.print("Time : "+job.get("Time"));
       }
   }
}catch (Throwable e){
   e.printStackTrace();
}

solved JSON parsing in android using inbuilt json library? [closed]