json syntax reference code example
Example: json syntax
ArrayList<Countries> listCountries = new ArrayList<>();
ArrayList<Detail> listDetails = new ArrayList<>();
ArrayList<Detail> listSub = new ArrayList<>();
Countries countries = new Countries();
countries.setId("1");
countries.setName("Sim");
countries.setGender("M");
countries.setCountry("khompong Chhnang");
countries.setPostalCode("225566");
listCountries.add(countries);
Detail detail = new Detail();
detail.setPhone("09659694146");
detail.setAddress("11H");
listDetails.add(detail);
Detail detail1 = new Detail();
detail1.setPhone("2222");
detail1.setAddress("tttt2");
listSub.add(detail1);
JSONObject jsonObject_sub = new JSONObject();
JSONArray jsonArray_sub = new JSONArray();
for (int i = 0; i < listSub.size(); i++){
try {
jsonObject_sub.put("Phone",listSub.get(i).getPhone());
jsonObject_sub.put("Address",listSub.get(i).getAddress());
jsonArray_sub.put(jsonObject_sub);
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject jb = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i<listDetails.size(); i++){
try {
jb.put("Address",listDetails.get(i).getAddress());
jb.put("Phone",listDetails.get(i).getPhone());
jb.put("sub_detail",jsonArray_sub);
} catch (JSONException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 3; i++){
jsonArray.put(jb);
}
JSONObject jsonObject = new JSONObject();
for (int i=0; i<listCountries.size(); i++){
try {
jsonObject.put("id",listCountries.get(i).getId());
jsonObject.put("name",listCountries.get(i).getName());
jsonObject.put("gender",listCountries.get(i).getGender());
jsonObject.put("detail",jsonArray);
jsonObject.put("country",listCountries.get(i).getCountry());
jsonObject.put("postal_code",listCountries.get(i).getPostalCode());
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(">>>", "jsonObject: "+jsonObject);
}
================Output===========================================================
{"id":"1","name":"Sim","gender":"M","detail":[{"Address":"11H","Phone":"09659694146","sub_detail":[{"Phone":"2222","Address":"tttt2"}]},{"Address":"11H","Phone":"09659694146","sub_detail":[{"Phone":"2222","Address":"tttt2"}]},{"Address":"11H","Phone":"09659694146","sub_detail":[{"Phone":"2222","Address":"tttt2"}]}],"country":"khompong Chhnang","postal_code":"225566"}