1

i have a problem. Navigation Data sent by Json but some data is dynamic.

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

coordinates in the geometric is this.

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

when type String in geometry is "LineString", coordinates in geometry changes.

but my geometry class is like this.

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

so i want to know how to get these dynamaic data by Gson ( java class )

in my think, i have to use deserialize, but i don't know..

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94

1 Answers1

3

You can try with JsonDeserializer to deserializer it as per JSON structure that is determined at run-time.

For more info have a look at GSON Deserialiser Example

Sample code: (parsing logic only for Geometry, modify it as per your need)

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));

output 1:

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

output 2:

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

For more clarity, please have a look at my another posts as well:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76