如何使用gson处理动态响应JSON数据?使用Gson和反序列化

如何使用gson处理动态响应JSON数据?使用Gson和反序列化,json,serialization,gson,Json,Serialization,Gson,我有个问题。 Json发送的导航数据,但有些数据是动态的 { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "c

我有个问题。 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类)获取这些动态数据

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

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

有关更多信息,请查看

示例代码:(仅解析几何图形的逻辑,根据需要进行修改)

产出2:

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

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


@Gergo Erdosi感谢编辑我的问题谢谢!!非常地令人惊叹的!!可信赖@布拉吉
{
  "type": "Point",
  "coordinates": [
    "127.032737",
    "37.260704"
  ]
}
{
  "type": "LineString",
  "coordinates": [
    "127.03268",
    "37.260549"
  ]
}