gpt4 book ai didi

java - 改造:将嵌套的 JSON 元素解析为列表

转载 作者:行者123 更新时间:2023-11-30 10:08:40 25 4
gpt4 key购买 nike

我在尝试将此 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"
}
}
]
}

这是包含地震详细信息的响应,所以基本上 “features” 中的每个 “properties” 都是我想要的 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 .它给了我错误

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

表明我正在尝试解析 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 - 改造:将嵌套的 JSON 元素解析为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com