There is a String containing this Json:

 {"type": "FeatureCollection", "source": "www.gmapgis.com", "features": [{"type":"Feature","properties": { "style": "#FF0000,5,1,#ff8800,0.4" }, "geometry": {"type": "Polygon", "coordinates": [ [[49.86686,40.40937],[49.86657,40.40917],[49.86733,40.40898],[49.86686,40.40937]]]}}]} 

How to parse the coordinate values?

  • can be used for example the library gson - Viktorov

1 answer 1

Here's an example of how to do this with org.json

 class Coordinate { private double lat; private double lon; public Coordinate(double lat, double lon){ this.lat = lat; this.lon = lon; } } 

The method that parses the json

 List<Coordinate> coordinateParser(String json) { JSONObject data = new JSONObject(json); JSONArray coordArr = data.getJSONArray("features") .getJSONObject(0) .getJSONObject("geometry") .getJSONArray("coordinates") .getJSONArray(0); List<Coordinate> coordinates = new ArrayList<>(); for (int i = 0; i < coordArr.length(); i++) { double lat = coordArr.getJSONArray(i).getDouble(0); double lon = coordArr.getJSONArray(i).getDouble(1); coordinates.add(new Coordinate(lat, lon)); } return coordinates; } 

In general, I would advise you to create a model object and using Jackson to serialize it automatically without additional manipulation.

  • one
    thanks!) really liked your answer - elik