【问题标题】:mergin different ArrayList types to ArrayList Object将不同的 ArrayList 类型合并到 ArrayList 对象
【发布时间】:2018-01-25 09:15:50
【问题描述】:

我下载了以下格式的 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 循环打印所有项目。

【问题讨论】:

    标签: java json arraylist


    【解决方案1】:

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

    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 位置。你可以随心所欲地使用它。

    【讨论】:

      【解决方案2】:

      快速回答 - 使用 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 字符串转换为 Java 对象。您可以使用不同的库。这是 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,这里是 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/

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 1970-01-01
        • 2016-01-05
        • 2022-01-22
        相关资源
        最近更新 更多