mergin不同的ArrayList类型为ArrayList Object

时间:2018-01-25 09:15:50

标签: java json arraylist

我下载了一个JSON文件,格式如下:

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

(这是实际文件http://zachtsou.webpages.auth.gr/FetchLocationData.php)。 我将它们存储在不同的ArrayLists中。 例如:

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

    }
}

我想制作一个常见的ArrayList,以便能够将其转移到新的活动(例如谷歌地图活动),并使用for循环打印所有项目。

2 个答案:

答案 0 :(得分:0)

快速回答 - 使用json库解析JSON字符串 - 示例如下。

您可以拥有一个对象 - 活动。添加getter和setter。

public class Activity {

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

public class LngLtd {

    private double latitude;
    private double longitude;
}

然后您需要将JSON String转换为Java Object。您可以使用不同的库。这是gson的例子。

你需要这种依赖。

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

然后转换

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

如果你有一个活动的集合(数组),它的方式是相同的

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

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

如果您使用的是另一个库,而不是gson,这里是杰克逊的一个例子。

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/

或者,如果您真的想要,可以像在代码示例中一样创建循环json对象,并逐个创建Activity对象,并将它们放入结果集合中。这家伙已在这里回答了。

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

答案 1 :(得分:0)

您可以为位置数据创建一个类,如下所示。

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


}

之后,解析您的JSONArray响应,如下所示

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

现在您有一个共同的ArrayList位置。你可以随意使用它。