Java – gson JSON parser array
•
Java
I want to parse some JSON and parse the array Unfortunately, I can't figure out how to handle nested arrays in JSON
JSON
{ "type": "MultiPolygon","coordinates": [ [ [ [ -71.25,42.33 ],[ -71.25,42.33 ] ] ],[ [ [ -71.23,[ -71.23,42.33 ] ] ] ] }
What did I achieve when I was just an array
public class JsonObjectBreakDown { public String type; public List<List<String[]>> coordinates = new ArrayList<>(); public void setCoordinates(List<List<String[]>> coordinates) { this.coordinates = coordinates; } }
Resolve call
JsonObjectBreakDown p = gson.fromJson(withDup,JsonObjectBreakDown.class);
Solution
You have a string array of arrays 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,[ -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]
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码