Java Gson Json解析器数组的数组
希望解析一些Json并解析出数组中的数组。不幸的是,我不知道如何处理json中的嵌套数组 json 当我只是一个数组时所实现的Java Gson Json解析器数组的数组,java,json,gson,Java,Json,Gson,希望解析一些Json并解析出数组中的数组。不幸的是,我不知道如何处理json中的嵌套数组 json 当我只是一个数组时所实现的 public class JsonObjectBreakDown { public String type; public List<List<String[]>> coordinates = new ArrayList<>(); public void setCoordinates(List<List
public class JsonObjectBreakDown {
public String type;
public List<List<String[]>> coordinates = new ArrayList<>();
public void setCoordinates(List<List<String[]>> coordinates) {
this.coordinates = coordinates;
}
}
您有一个字符串数组数组数组。你需要
public List<List<List<String[]>>> coordinates = new ArrayList<>();
基本上,您必须迭代每个数组,并获得嵌套数组。将每个数组视为它自己的对象。所以你得到对象,然后从中得到数组对象,依此类推。。。
JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class);
public List<List<List<String[]>>> coordinates = new ArrayList<>();
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;
}
}
[-71.25, 42.33]