showing results for - "learn json"
Roberta
30 Jun 2020
1ArrayList<Countries> listCountries = new ArrayList<>();//country
2        ArrayList<Detail> listDetails = new ArrayList<>();//listDetail
3        ArrayList<Detail> listSub =  new ArrayList<>();//List Sub
4
5        Countries countries = new Countries();
6        countries.setId("1");
7        countries.setName("Sim");
8        countries.setGender("M");
9        countries.setCountry("khompong Chhnang");
10        countries.setPostalCode("225566");
11        //Add Countries object to ArrayList
12        listCountries.add(countries);
13
14        Detail detail =  new Detail();
15        detail.setPhone("09659694146");
16        detail.setAddress("11H");
17        //Add Detail object to ArrayList
18        listDetails.add(detail);
19
20        Detail detail1 =  new Detail();
21        detail1.setPhone("2222");
22        detail1.setAddress("tttt2");
23        listSub.add(detail1);
24
25        JSONObject jsonObject_sub = new JSONObject();
26        JSONArray jsonArray_sub = new JSONArray();
27        for (int i = 0; i < listSub.size(); i++){
28            try {
29                jsonObject_sub.put("Phone",listSub.get(i).getPhone());
30                jsonObject_sub.put("Address",listSub.get(i).getAddress());
31                jsonArray_sub.put(jsonObject_sub);
32            } catch (JSONException e) {
33                e.printStackTrace();
34            }
35        }
36
37        JSONObject jb = new JSONObject();
38        JSONArray jsonArray = new JSONArray();
39        for (int i = 0; i<listDetails.size(); i++){
40            //convert to JSONObject
41            try {
42                jb.put("Address",listDetails.get(i).getAddress());
43                jb.put("Phone",listDetails.get(i).getPhone());
44                jb.put("sub_detail",jsonArray_sub);
45            } catch (JSONException e) {
46                e.printStackTrace();
47            }
48        }
49        //convert JSONObject to JSONArray
50        for (int i = 0; i < 3; i++){
51            jsonArray.put(jb);
52        }
53
54
55        JSONObject jsonObject = new JSONObject();
56        for (int i=0; i<listCountries.size(); i++){
57            try {
58                //convert to JSONObject
59                jsonObject.put("id",listCountries.get(i).getId());
60                jsonObject.put("name",listCountries.get(i).getName());
61                jsonObject.put("gender",listCountries.get(i).getGender());
62                //put JSONArray into JSONObject
63                jsonObject.put("detail",jsonArray);
64                jsonObject.put("country",listCountries.get(i).getCountry());
65                jsonObject.put("postal_code",listCountries.get(i).getPostalCode());
66            } catch (JSONException e) {
67                e.printStackTrace();
68            }
69            Log.d(">>>", "jsonObject: "+jsonObject);
70        }
71
72
73================Output===========================================================
74  {"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"}