showing results for - "how to wirte json format"
Darryn
19 Aug 2020
1JSONArray jArray = new JSONArray();
2        Countries count =  new Countries();
3        count.setNum1(-90.0715);
4        count.setNum2(29.9510);
5        ArrayList<Countries> ll_conn = new ArrayList<>();
6        ll_conn.add(count);
7        for (int i = 0; i< ll_conn.size(); i++){
8            try {
9                jArray.put(ll_conn.get(i).getNum1());
10                jArray.put(ll_conn.get(i).getNum2());
11            } catch (JSONException e) {
12                e.printStackTrace();
13            }
14        }
15
16
17
18
19        JSONObject jsonObject2  = new JSONObject();
20        ArrayList<Countries> list2 = new ArrayList<>();
21        Countries countries = new Countries();
22        countries.setType("Point");
23        list2.add(countries);
24        for (int i = 0; i < list2.size(); i++){
25            try {
26                jsonObject2.put("type",list2.get(i).getType());
27                jsonObject2.put("coordinates",jArray);
28            } catch (JSONException e) {
29                e.printStackTrace();
30            }
31        }
32
33
34        JSONObject jsonObject1 =  new JSONObject();
35        JSONArray jsonArray1 = new JSONArray();
36        Countries countries1 = new Countries();
37        countries1.setType("Feature");
38        ArrayList<Countries> ll_count1 = new ArrayList<>();
39        ll_count1.add(countries1);
40        for (int i = 0; i < ll_count1.size(); i++){
41            try {
42                jsonObject1.put("type",ll_count1.get(i).getType());
43                jsonObject1.put("geometry",jsonObject2);
44
45            } catch (JSONException e) {
46                e.printStackTrace();
47            }
48        }
49        for (int i = 0; i <3; i++){
50            jsonArray1.put(jsonObject1);
51        }
52
53        JSONObject jsonObject =  new JSONObject();
54        Countries obj_coun = new Countries();
55        ArrayList<Countries> ll_Cout = new ArrayList<>();
56        obj_coun.setType("FeatureCollection");
57        //add all data respone from object countries into ArrayList type of Countries
58        ll_Cout.add(obj_coun);
59        for (int i = 0; i < ll_Cout.size(); i++) {
60            try {
61                jsonObject.put("type", ll_Cout.get(i).getType());
62                jsonObject.put("features",jsonArray1);
63            } catch (JSONException e) {
64                e.printStackTrace();
65            }
66        }
67        Log.d(">>>", "jsonObject: "+jsonObject);
68
69
70==============OutPut================================================
71  {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-90.0715,29.951]}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-90.0715,29.951]}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-90.0715,29.951]}}]}