-3

i am beginner at android can anybody tell me how to parse All Json arrays that come under one fix array i want to parse all array name in parents

Following is the sample Json:

{
  "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"
        }
      ]
    }
  ]
} 
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61

4 Answers4

1

You can parse the JSON using Android's native JSON parser as below:

public ParsedData parseWorksheet(String in) throws JSONException {
        JSONObject reader = new JSONObject(in);
        String errCode = reader.getString("err-code");
        JSONArray jsonArrayOuter = reader.optJSONArray("worksheet");
        ParsedData parsedData = new ParsedData();
        parsedData.errCode = errCode;
        for(int i=0; i < jsonArrayOuter.length(); i++){
          JSONObject jsonObject = jsonArrayOuter.getJSONObject(i);
          Iterator<String> iterator = jsonObject.keys();
          ArrayList<Worksheet> worksheetArrayList = new ArrayList<Worksheet>();
          while(iterator.hasNext()){
              Worksheet worksheet= new Worksheet();
               String key = (String)iterator.next();
               worksheet.worksheetDataKey = key;
               JSONArray jsonArrayW =  jsonObject.optJSONArray(key);
               WorksheetInnerData[] WorksheetDataArray = new WorksheetInnerData[jsonArrayW.length()];
               for(int j=0; j < jsonArrayW.length(); j++){
                  JSONObject jsonObjectWorksheetData = jsonArrayW.getJSONObject(j);
                  WorksheetInnerData worksheetData= new WorksheetInnerData();
                  String worksheet_id = jsonObjectWorksheetData.getString("worksheet_id");
                  worksheetData.worksheet_id = worksheet_id;
                  String job_no = jsonObjectWorksheetData.getString("job_no");
                  worksheetData.job_no = job_no;
                  String message = jsonObjectWorksheetData.getString("message");
                  worksheetData.message = message;
                  String added_date =  jsonObjectWorksheetData.getString("added_date");
                  worksheetData.added_date = added_date;
                  String status = jsonObjectWorksheetData.getString("status");
                  worksheetData.status = status;
                  WorksheetDataArray[j] = worksheetData;
               }
               worksheet.WorksheetInnerDataArray = WorksheetDataArray;
               worksheetArrayList.add(worksheet);
           }
          parsedData.worksheetArray = worksheetArrayList;
       }
      return parsedData ;
    }

Following should be your data model:

private static class ParsedData {
    public String errCode;
    public ArrayList<Worksheet> worksheetArray;
}

private static class Worksheet {
    public String worksheetDataKey;
    public WorksheetInnerData WorksheetInnerDataArray [];
}

private static class WorksheetInnerData {
    public String worksheet_id;
    public String job_no;
    public String message;
    public String added_date;
    public String status;
}

Hope it helps!!!

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
1

its very simple to parse json data to file, 1).you need first String object if it true

2)then take JsonArray with its object to get "worksheet" array the 3).now your question is :you have huge json arrays then a).take Iterator for get all array keys by Iterator and save it to arraylist. b).and string object pass it to the JSONArray(Stringkey); c)now get all strings within json array using loop d)and save all to ..where you want to save finish..

this is the way you can parse eaisly

harsh
  • 37
  • 9
0
   Try This

         RequestQueue queue = Volley.newRequestQueue(this); 
       final String url = "http://httpbin.org/get?param1=hello";

    //prepare the Request
   JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() 
{
    @Override
    public void onResponse(JSONObject response) {   
  JSONArray responserecentbooks = response.getJSONArray("arrayname");
                for (int i = 0; i < responserecentbooks.length(); i++) {
                    JSONObject person = (JSONObject) responserecentbooks.get(i);
                    String bookid = person.getString("element");
                   //store values in Arraylist 
                    try {

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        Log.d("Response", response.toString());
    }
}, 
new Response.ErrorListener() 
{
     @Override
     public void onErrorResponse(VolleyError error) {            
        Log.d("Error.Response", response);
   }
}
);

 // add it to the RequestQueue   
 queue.add(getRequest);
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0
  1. Add compile 'com.google.code.gson:gson:2.2.4' at app level build.gradle.
  2. Create same class structure like JSON
  3. new Gson().fromJson(jsonArray.toString(), new TypeToken>() { }.getType());