【问题标题】:how to use gson handle dynamic response JSON data? using Gson and deserialize如何使用 gson 处理动态响应 JSON 数据?使用 Gson 和反序列化
【发布时间】:2014-06-26 20:07:44
【问题描述】:

我有问题。 Json 发送的导航数据,但有些数据是动态的。

{
        "type": "FeatureCollection",
        "features": [
              {
                    "type": "Feature",
                    "geometry": {
                          "type": "Point",
                          "coordinates": [
                    127.032737,
                    37.260704
                ]        
            }
}

几何坐标是这样的。

{
                    "type": "Feature",
                    "geometry": {
                          "type": "LineString",
                          "coordinates": [
                                [
                        127.032680,
                        37.260549
                    ],
                                [
                        127.032680,
                        37.260549
                    ]        
                ]        
            }
}

当几何中的字符串类型为“LineString”时,几何中的坐标会发生变化。

但我的几何课是这样的。

public class Geometry {
    String type = "";
    public List<String> coordinates;
}

所以我想知道如何通过 Gson(java 类)获取这些动态数据

在我看来,我必须使用反序列化,但我不知道..

【问题讨论】:

  • @Gergo Erdosi 感谢编辑我的问题

标签: json serialization gson


【解决方案1】:

您可以尝试使用 JsonDeserializer 根据运行时确定的 JSON 结构对其进行反序列化。

更多信息请查看GSON Deserialiser Example

示例代码:(只为几何解析逻辑,根据需要修改

class Geometry {
    private String type = "";
    private List<String> coordinates;
    // getter & setter
}

class GeometryDeserializer implements JsonDeserializer<Geometry> {

    @Override
    public Geometry deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {

        Geometry geometry = new Geometry();

        JsonObject jsonObject = json.getAsJsonObject();

        JsonElement features = jsonObject.get("features");
        String type = "";
        List<String> list = new ArrayList<String>();
        if (features != null) {
            type = features.getAsJsonArray().get(0).getAsJsonObject().get("geometry")
                    .getAsJsonObject().get("type").getAsString();
        } else {
            type = jsonObject.get("geometry").getAsJsonObject().get("type").getAsString();
        }

        if ("LineString".equals(type)) {
            JsonArray coordinates = jsonObject.get("geometry").getAsJsonObject()
                    .get("coordinates").getAsJsonArray();
            for (int i = 0; i < coordinates.size(); i++) {
                list.add(coordinates.get(i).getAsJsonArray().get(i).getAsString());
            }
        } else {
            JsonArray coordinates = features.getAsJsonArray().get(0).getAsJsonObject()
                    .get("geometry").getAsJsonObject().get("coordinates").getAsJsonArray();
            for (int i = 0; i < coordinates.size(); i++) {
                list.add(coordinates.get(i).getAsString());
            }
        }
        geometry.setCoordinates(list);
        geometry.setType(type);
        return geometry;
    }
}

Geometry data = new GsonBuilder()
        .registerTypeAdapter(Geometry.class, new GeometryDeserializer()).create()
        .fromJson(jsonString, Geometry.class);

System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

输出 1:

{
  "type": "Point",
  "coordinates": [
    "127.032737",
    "37.260704"
  ]
}

输出2:

{
  "type": "LineString",
  "coordinates": [
    "127.03268",
    "37.260549"
  ]
}

为了更清楚,请查看我的其他帖子:

【讨论】:

  • 谢谢!!非常!!惊人的!!难以置信!! @Braj
猜你喜欢
  • 2012-02-10
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 2020-05-12
  • 2017-01-27
  • 1970-01-01
相关资源
最近更新 更多