加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Gson Json解析器数组数组

发布时间:2020-12-14 05:38:16 所属栏目:Java 来源:网络整理
导读:希望解析一些Json并解析数组数组.不幸的是我无法弄清楚如何处理json中的嵌套数组. JSON { "type": "MultiPolygon","coordinates": [ [ [ [ -71.25,42.33 ],[ -71.25,42.33 ] ] ],[ [ [ -71.23,[ -71.23,42.33 ] ] ] ]} 当我只是一个阵列时,我实现了什么. pub
希望解析一些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]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读