Parser Gson Json Array of Arrays

Looking at parsing some Json and analyzing an array of arrays. Unfortunately, I cannot figure out how to handle nested arrays in json.

Json

{ "type": "MultiPolygon", "coordinates": [ [ [ [ -71.25, 42.33 ], [ -71.25, 42.33 ] ] ], [ [ [ -71.23, 42.33 ], [ -71.23, 42.33 ] ] ] ] } 

What I implemented when I'm just a single array.

 public class JsonObjectBreakDown { public String type; public List<List<String[]>> coordinates = new ArrayList<>(); public void setCoordinates(List<List<String[]>> coordinates) { this.coordinates = coordinates; } } 

parsing session

 JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class); 
+6
source share
1 answer

You have an array of arrays of arrays of arrays of strings . You need

 public List<List<List<String[]>>> coordinates = new ArrayList<>(); 

Following

 public static void main(String args[]) { Gson gson = new Gson(); String jsonstr ="{ \"type\": \"MultiPolygon\",\"coordinates\": [ [ [ [ -71.25, 42.33 ], [ -71.25, 42.33 ] ] ], [ [ [ -71.23, 42.33 ], [ -71.23, 42.33 ] ] ] ]}"; JsonObjectBreakDown obj = gson.fromJson(jsonstr, JsonObjectBreakDown.class); System.out.println(Arrays.toString(obj.coordinates.get(0).get(0).get(0))); } public static class JsonObjectBreakDown { public String type; public List<List<List<String[]>>> coordinates = new ArrayList<>(); public void setCoordinates(List<List<List<String[]>>> coordinates) { this.coordinates = coordinates; } } 

prints

 [-71.25, 42.33] 
+10
source

Source: https://habr.com/ru/post/954206/


All Articles