简体   繁体   中英

mergin different ArrayList types to ArrayList Object

I download a JSON file with following format:

{
   "name": "enter_username",
   "longitude": "22.601952",
   "latitude": "40.674065",
   "type": "Big_Pothole"
}

(this is the actual file http://zachtsou.webpages.auth.gr/FetchLocationData.php ). And I store them in different ArrayLists . For example :

private void getDBLocations(JSONArray j){
    dblocations = new ArrayList<LatLng>();
    name = new ArrayList<>();
    types = new ArrayList<String>();
    full= new ArrayList<>();
    // Traversing through all the items in the Json array
    for(int i=0;i<j.length();i++){
        try{
            //Getting json object
            JSONObject json = j.getJSONObject(i);

            // Adding types to array list
            names.add(json.getString(Config.TAG_NAME));
            type.add(json.getString(Config.TAG_TYPE));
            longitude.add(json.getDouble(Config.TAG_LONGITUDE));
            latitude.add(json.getDouble(Config.TAG_LATITUDE));

        }catch (JSONException e){
            e.printStackTrace();
        }

        // Combining Longitude and Latitude on ArrayList<LatLon>
        dblocations.add(new LatLng(latitude.get(i), longitude.get(i)));

    }
}

I would like to make one common ArrayList so to be able to transfer it to new activity(for example google maps activity) and print all the items with a for loop.

Quick answer - use a json library to parse a JSON String - example bellow.

You can have one Object - Activity. Add getters and setters.

public class Activity {

    private String name;
    private String type;
    private LngLtd location;
}

public class LngLtd {

    private double latitude;
    private double longitude;
}

Then you need convert JSON String to Java Object. You can use different libraries. This is example of gson.

You need this dependency.

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'

And then convertion

 Gson gson=new Gson();
 Activity parsedActivity =gson.fromJson(jsonString, Activity.class);

If you have a collection (array) of activities, it goes the same way

Activity[] activities = gson.fromJson(jsonString, Activity[].class);

https://stackoverflow.com/a/17300003/4587961

If you use another library, rather than gson, here is an example of Jackson.

ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{\n   \"name\": \"enter_username\",\n\"longitude\": \"22.601952\",\n\"latitude\": \"40.674065\",\n\"type\": \"Big_Pothole\"\n}";//Your JSON String;

//JSON from file to Object
Activity activity = mapper.readValue(jsonInString, Activity.class);

https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/

Or if you really want, you can create loop through json objects, as you do in your code example, and create Activity object one by one, and put them into the result collection. This guy has answered here.

https://stackoverflow.com/a/48439517/4587961

You can create one class for Location data like below..

public class LocationData {

private String name;
private String longitude;
private String latitude;
private String type;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

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;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}


}

After that, parse your JSONArray response as below

private void getDBLocations(JSONArray j){
ArrayList<LocationData> locations = new ArrayList<LocationData>();

// Traversing through all the items in the Json array
for(int i=0;i<j.length();i++){
    try{
        //Getting json object
        JSONObject json = j.getJSONObject(i);

        LocationData location = new LocationData();            
        location.setName(json.getString(Config.TAG_NAME));
        location.setType(json.getString(Config.TAG_TYPE));
        location.setLongitude(json.getDouble(Config.TAG_LONGITUDE));
        location.setLatitude(json.getDouble(Config.TAG_LATITUDE));

        locations.add(location);
    }catch (JSONException e){
        e.printStackTrace();
    }
}
}

Now you have one common ArrayList of locations. You can use it as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM