format json with @ code example

Example 1: how to wirte json format

JSONArray jArray = new JSONArray();
        Countries count =  new Countries();
        count.setNum1(-90.0715);
        count.setNum2(29.9510);
        ArrayList<Countries> ll_conn = new ArrayList<>();
        ll_conn.add(count);
        for (int i = 0; i< ll_conn.size(); i++){
            try {
                jArray.put(ll_conn.get(i).getNum1());
                jArray.put(ll_conn.get(i).getNum2());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }




        JSONObject jsonObject2  = new JSONObject();
        ArrayList<Countries> list2 = new ArrayList<>();
        Countries countries = new Countries();
        countries.setType("Point");
        list2.add(countries);
        for (int i = 0; i < list2.size(); i++){
            try {
                jsonObject2.put("type",list2.get(i).getType());
                jsonObject2.put("coordinates",jArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        JSONObject jsonObject1 =  new JSONObject();
        JSONArray jsonArray1 = new JSONArray();
        Countries countries1 = new Countries();
        countries1.setType("Feature");
        ArrayList<Countries> ll_count1 = new ArrayList<>();
        ll_count1.add(countries1);
        for (int i = 0; i < ll_count1.size(); i++){
            try {
                jsonObject1.put("type",ll_count1.get(i).getType());
                jsonObject1.put("geometry",jsonObject2);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i <3; i++){
            jsonArray1.put(jsonObject1);
        }

        JSONObject jsonObject =  new JSONObject();
        Countries obj_coun = new Countries();
        ArrayList<Countries> ll_Cout = new ArrayList<>();
        obj_coun.setType("FeatureCollection");
        //add all data respone from object countries into ArrayList type of Countries
        ll_Cout.add(obj_coun);
        for (int i = 0; i < ll_Cout.size(); i++) {
            try {
                jsonObject.put("type", ll_Cout.get(i).getType());
                jsonObject.put("features",jsonArray1);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Log.d(">>>", "jsonObject: "+jsonObject);


==============OutPut================================================
  {"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]}}]}

Example 2: JSON format

ArrayList<Countries> listCountries = new ArrayList<>();//country
        ArrayList<Detail> listDetails = new ArrayList<>();//listDetail
        ArrayList<Detail> listSub =  new ArrayList<>();//List Sub

        Countries countries = new Countries();
        countries.setId("1");
        countries.setName("Sim");
        countries.setGender("M");
        countries.setCountry("khompong Chhnang");
        countries.setPostalCode("225566");
        //Add Countries object to ArrayList
        listCountries.add(countries);

        Detail detail =  new Detail();
        detail.setPhone("09659694146");
        detail.setAddress("11H");
        //Add Detail object to ArrayList
        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++){
            //convert to JSONObject
            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();
            }
        }
        //convert JSONObject to JSONArray
        for (int i = 0; i < 3; i++){
            jsonArray.put(jb);
        }


        JSONObject jsonObject = new JSONObject();
        for (int i=0; i<listCountries.size(); i++){
            try {
                //convert to JSONObject
                jsonObject.put("id",listCountries.get(i).getId());
                jsonObject.put("name",listCountries.get(i).getName());
                jsonObject.put("gender",listCountries.get(i).getGender());
                //put JSONArray into JSONObject
                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"}