java - 改造:将嵌套的 JSON 元素解析为列表
问题描述
我在尝试将此 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
}
我试过做这里建议的自定义反序列化。它给了我错误
预期为 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);
}
}
关于这里发生了什么的任何想法?
解决方案
您可以为您的 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 的回答了解如何操作。
现在您可以通过创建响应类的对象来获得任何东西,并且通过该对象您可以访问您想要的任何属性。谢谢。
推荐阅读
- java - 面临使用 selenium webdriver 处理两个以上窗口的问题
- java - 在 Java Web 应用程序中引发未知异常
- c# - 此函数只能在 Entity Framework 更新后由 LINQ to Entities 调用
- c++ - 如何检查 int 变量是否包含合法(不是陷阱表示)值?
- javascript - 在 PHP 中使用带有 CSS 和 JavaScript 的长 HTML 在 PhpStorm 中生成错误
- parsing - Haskell 解析递归和可能
- excel - Excel 自动化记录响应
- redis - 队列或其他方法来处理分时数据?
- java - 是否可以从其他类访问主活动中的插页式广告?
- odoo-9 - Odoo:在取消链接方法上询问删除原因?