Gson Json解析器阵列数组

use*_*908 6 java json gson

希望解析一些Json并解析数组数组.不幸的是我无法弄清楚如何处理json中的嵌套数组.

JSON

{
    "type": "MultiPolygon",
    "coordinates": [
        [
            [
                [
                    -71.25,
                    42.33
                ],
                [
                    -71.25,
                    42.33
                ]
            ]
        ],
        [
            [
                [
                    -71.23,
                    42.33
                ],
                [
                    -71.23,
                    42.33
                ]
            ]
        ]
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

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




}
Run Code Online (Sandbox Code Playgroud)

解析呼叫

JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class);
Run Code Online (Sandbox Code Playgroud)

Sot*_*lis 10

你有一组数组的字符串数组数组.你需要

public List<List<List<String[]>>> coordinates = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

下列

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

版画

[-71.25, 42.33]
Run Code Online (Sandbox Code Playgroud)