2

The data is unknown amounts of JSONobjects, I'd like to capture three data, NAME, LONGITUDE and LATITUDE into the Arraylist.

This is the result of the following code.

wrapall:[[title1, longitude1, latitude1, title2, longitude2, latitude2, title3, longitude3, latitude3, title4,.......]]

But this is what I expect.

wrapall:[[title1, longitude1, latitude1],[title2, longitude2, latitude2],[title3, longitude3, latitude3],.......]

What should be done to change the format like this?

private ArrayList<ArrayList<String>> wrapall =new ArrayList<>();
private ArrayList<String> transthree = new ArrayList<>();

private void parserJson(JSONArray jsonArray) {

    try {
        JSONArray array = jsonArray;
        Log.d("jsonarrayishere", array.toString());

        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);
            String title = object.getString("NAME");
            String longitude = object.get("LONGITUDE").toString();
            String latitude = object.get("LATITUDE").toString();

                transthree.add(title);
                transthree.add(longitude);
                transthree.add(latitude);

        }

        wrapall.add(transthree);
        transthree = new ArrayList<>();

     Log.d("transthree",transthree.toString());

     Log.d("wrapall", wrapall.toString());

    } catch (JSONException e) {
        e.printStackTrace();
        Log.d("objectstestwrong", e.getLocalizedMessage());
    }


}
Meowmicanfly
  • 49
  • 1
  • 7

5 Answers5

3

better to create a POJO class

SAMPLE CODE

Try this

Create a model class like this

public class DataModel
{
    String title;
    String longitude,latitude;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }
}

Than use this

private ArrayList<DataModel> wrapall = new ArrayList<>();

private void parserJson (JSONArray jsonArray){

    try {
        JSONArray array = jsonArray;
        Log.d("jsonarrayishere", array.toString());

        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);

            DataModel dataModel = new DataModel();

            dataModel.setTitle(object.getString("NAME"));
            dataModel.setLatitude(object.get("LATITUDE").toString());
            dataModel.setLongitude(object.get("LONGITUDE").toString());

            //Add data model to array list
            wrapall.add(dataModel);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        Log.d("objectstestwrong", e.getLocalizedMessage());
    }
}

Use this to Retrieve data from list

    for (int i=0;i<wrapall.size();i++){
        Log.e("NAME",wrapall.get(i).getTitle());
        Log.e("LONGITUDE",wrapall.get(i).getLongitude());
        Log.e("LATITUDE",wrapall.get(i).getLatitude());
    }
KbiR
  • 4,047
  • 6
  • 37
  • 103
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Create a POJO class add this three property

public class TestData{
    private String title;
    private String longitude;
    private String latitude;

    //create constructor and getter, setter
}

Create a list List<YOUR_POJO>. add pojo object to the list. after that convert the list to JSON String

List<TestData> dataList = new ArrayList<>();
dataList.add(new TestData(title, lat, long));
Empty Brain
  • 627
  • 8
  • 24
1
        JSONArray array = jsonArray;
    Log.d("jsonarrayishere", array.toString());

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        String title = object.getString("NAME");
        String longitude = object.get("LONGITUDE").toString();
        String latitude = object.get("LATITUDE").toString();

        transthree.add(title);
        transthree.add(longitude);
        transthree.add(latitude);

        // Here
        wrapall.add(transthree);
        transthree.clear();
    }
Liar
  • 1,235
  • 1
  • 9
  • 19
1

create new instance of array list inside the for loop &

also add that object to the wrapall arraylist inside for loop;

you did both these tasks outside for loop & hence wrong output;

Anjani Mittal
  • 507
  • 1
  • 7
  • 19
0
String title="title",longi="longitude",lati="latitude";
 String result="[";
            for(int i=0;i<2;i++){
            result=result+"[";
            result=result+title+","+longi+","+lati;
            result=result+"],";
            }
            result=result+"]";
            Log.d("Your Desired String",result);
Syed Danish Haider
  • 1,334
  • 11
  • 15