Android- create JSON Array and JSON Object Android- create JSON Array and JSON Object android android

Android- create JSON Array and JSON Object


Use the following code:

JSONObject student1 = new JSONObject();try {    student1.put("id", "3");    student1.put("name", "NAME OF STUDENT");    student1.put("year", "3rd");    student1.put("curriculum", "Arts");    student1.put("birthday", "5/5/1993");} catch (JSONException e) {    // TODO Auto-generated catch block    e.printStackTrace();}JSONObject student2 = new JSONObject();try {    student2.put("id", "2");    student2.put("name", "NAME OF STUDENT2");    student2.put("year", "4rd");    student2.put("curriculum", "scicence");    student2.put("birthday", "5/5/1993");} catch (JSONException e) {    // TODO Auto-generated catch block    e.printStackTrace();}JSONArray jsonArray = new JSONArray();jsonArray.put(student1);jsonArray.put(student2);JSONObject studentsObj = new JSONObject();    studentsObj.put("Students", jsonArray);String jsonStr = studentsObj.toString();    System.out.println("jsonString: "+jsonStr);


public JSONObject makJsonObject(int id[], String name[], String year[],            String curriculum[], String birthday[], int numberof_students)            throws JSONException {        JSONObject obj = null;        JSONArray jsonArray = new JSONArray();        for (int i = 0; i < numberof_students; i++) {            obj = new JSONObject();            try {                obj.put("id", id[i]);                obj.put("name", name[i]);                obj.put("year", year[i]);                obj.put("curriculum", curriculum[i]);                obj.put("birthday", birthday[i]);            } catch (JSONException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            jsonArray.put(obj);        }        JSONObject finalobject = new JSONObject();        finalobject.put("student", jsonArray);        return finalobject;    }


 JSONObject obj = new JSONObject();            try {                obj.put("id", "3");                obj.put("name", "NAME OF STUDENT");                obj.put("year", "3rd");                obj.put("curriculum", "Arts");                obj.put("birthday", "5/5/1993");            } catch (JSONException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }             JSONArray js=new JSONArray(obj.toString());             JSONObject obj2 = new JSONObject();             obj2.put("student", js.toString());