微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Gson Json解析器数组数组

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

JSON

{
    "type": "Multipolygon","coordinates": [
        [
            [
                [
                    -71.25,42.33
                ],[
                    -71.25,42.33
                ]
            ]
        ],[
            [
                [
                    -71.23,[
                    -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);

解决方法

你有一组数组的字符串数组数组.你需要
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,[                    -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]

原文地址:https://www.jb51.cc/java/121818.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐