java – Gson Json解析器数组数组

前端之家收集整理的这篇文章主要介绍了java – Gson Json解析器数组数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
希望解析一些Json并解析数组数组.不幸的是我无法弄清楚如何处理json中的嵌套数组.

JSON

  1. {
  2. "type": "MultiPolygon","coordinates": [
  3. [
  4. [
  5. [
  6. -71.25,42.33
  7. ],[
  8. -71.25,42.33
  9. ]
  10. ]
  11. ],[
  12. [
  13. [
  14. -71.23,[
  15. -71.23,42.33
  16. ]
  17. ]
  18. ]
  19. ]
  20. }

当我只是一个阵列时,我实现了什么.

  1. public class JsonObjectBreakDown {
  2. public String type;
  3. public List<List<String[]>> coordinates = new ArrayList<>();
  4. public void setCoordinates(List<List<String[]>> coordinates) {
  5. this.coordinates = coordinates;
  6. }
  7.  
  8.  
  9.  
  10.  
  11. }

解析呼叫

  1. JsonObjectBreakDown p = gson.fromJson(withDup,JsonObjectBreakDown.class);

解决方法

你有一组数组的字符串数组数组.你需要
  1. public List<List<List<String[]>>> coordinates = new ArrayList<>();

下列

  1. public static void main(String args[]) {
  2. Gson gson = new Gson();
  3. String jsonstr ="{ \"type\": \"MultiPolygon\",\"coordinates\": [ [ [ [ -71.25,42.33 ],[ -71.25,42.33 ] ] ],[ [ [ -71.23,[ -71.23,42.33 ] ] ] ]}";
  4. JsonObjectBreakDown obj = gson.fromJson(jsonstr,JsonObjectBreakDown.class);
  5.  
  6. System.out.println(Arrays.toString(obj.coordinates.get(0).get(0).get(0)));
  7. }
  8.  
  9. public static class JsonObjectBreakDown {
  10. public String type;
  11. public List<List<List<String[]>>> coordinates = new ArrayList<>();
  12. public void setCoordinates(List<List<List<String[]>>> coordinates) {
  13. this.coordinates = coordinates;
  14. }
  15. }

版画

  1. [-71.25,42.33]

猜你在找的Java相关文章