7

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

json

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

当我只有一个数组时我已经实现了。

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




}

解析调用

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

1 回答 1

11

你有一个由 Strings 数组组成的数组。你需要

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]
于 2013-09-19T22:49:41.847 回答