改造:将嵌套的JSON元素解析到列表中

时间:2018-12-07 07:49:52

标签: java android json api retrofit

我在尝试将此JSON响应解析为"properties"元素列表时遇到困难。我的JSON看起来像这样:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "mag": 6.6,
                "place": "192km ESE of Tadine, New Caledonia"
            }
        },
        {
            "type": "Feature",
            "properties": {
                "mag": 7.5,
                "place": "168km ESE of Tadine, New Caledonia"
            }
        },
        {
            "type": "Feature",
            "properties": {
                "mag": 6,
                "place": "155km ESE of Tadine, New Caledonia"
            }
        }
    ]
}

此响应包含地震详细信息,因此"properties"中的每个"features"基本上都是我想要的POJO,但所有这些仅位于列表中。这是我的Earthquake课:

public class Earthquake {
    @SerializedName("mag")
    private double magnitude;
    @SerializedName("place")
    private String location;

    public   Earthquake(double magnitude, String location) {
        this.magnitude = magnitude;
        this.location = location;
    }
    // getters
}

我尝试进行建议here的自定义反序列化。它给了我错误

  

期望BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT中

建议我正在尝试解析JsonObject而不是JsonArray。这是我使用的解串器。

public class EarthquakeDeserializer implements JsonDeserializer<ArrayList<Earthquake>> {

    @Override
    public ArrayList<Earthquake> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        // get list of "features"
        JsonElement features = json.getAsJsonObject().get("features");

        JsonArray earthquakeElements = new JsonArray();
        for (JsonElement feature : features.getAsJsonArray()){
            JsonElement properties = feature.getAsJsonObject().get("properties");
            earthquakeElements.add(properties);
        }

        Type listType = new TypeToken<ArrayList<Earthquake>>(){}.getType();

        return new Gson().fromJson(earthquakeElements, listType);
    }
}

关于这里发生的事情有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以为您的Json创建这种POJO类,无论您只想要响应主体的哪一部分,都需要为整个响应创建POJO,并且需要从该POJO中获取适当的属性。 ->

这是您的主要json对象->

public class Example {

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("features")
    @Expose
    private List<Feature> features = null;

    public String getType() {
        return type;
    }

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

    public List<Feature> getFeatures() {
        return features;
    }

    public void setFeatures(List<Feature> features) {
        this.features = features;
    }

}

这是您的要素类->

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Feature {

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("properties")
    @Expose
    private Properties properties;

    public String getType() {
        return type;
    }

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

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

}

这是您的属性类->

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Properties {

    @SerializedName("mag")
    @Expose
    private Integer mag;
    @SerializedName("place")
    @Expose
    private String place;

    public Integer getMag() {
        return mag;
    }

    public void setMag(Integer mag) {
        this.mag = mag;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

}

创建此类后,您可以通过GSON库将JSON序列化为POJO,有关具体操作,请参考HussainAbbas的答案。

现在,您可以通过创建响应类的对象来获取任何内容,并且可以通过该对象访问所需的任何属性。谢谢。

答案 1 :(得分:0)

检查一下

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("type")
@Expose
private String type;
@SerializedName("features")
@Expose
private List<Feature> features = null;

  public String getType() {
    return type;
  }

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

  public List<Feature> getFeatures() {
    return features;
  }

  public void setFeatures(List<Feature> features) {
   this.features = features;
  }

}

----------------------------------- com.example.Feature.java ----- ------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Feature {

 @SerializedName("type")
 @Expose
 private String type;
 @SerializedName("properties")
 @Expose
 private Properties properties;

 public String getType() { 
   return type;
 }

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

 public Properties getProperties() {
   return properties;
 }

 public void setProperties(Properties properties) {
   this.properties = properties; 
 }

} ----------------------------------- com.example.Properties.java -------- ---------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Properties {

   @SerializedName("mag")
   @Expose
   private Integer mag;
   @SerializedName("place")
   @Expose
   private String place;

   public Integer getMag() {
       return mag;
   }

   public void setMag(Integer mag) {
      this.mag = mag;
   } 

   public String getPlace() {
     return place;
   }

   public void setPlace(String place) {
     this.place = place;
   }

 }

在此之后,在您的改造响应中添加...

Gson gson = new Gson()
String data = gson.toJson(response.body());
LoginResponse loginResponse = gson.fromJson(dataString,LoginResponse.class);
Example example = gson.fromJson(dataString,Example.class);
String feature_proprerties_mag = example.getFeatures().getProperties().getMag