本文介绍了Gson Json解析器数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





  {
type: MultiPolygon,
坐标:[
[
[
[
-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); 


解决方案

您有一个阵列数组的字符串数组的数组。你需要

  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 []>>>坐标){
this.coordinates = coordinates;




打印

  [ -  71.25,42.33] 


Looking to parse some Json and parse out array of arrays. Unfortunately I cannot figure out how to handle nested arrays within the json.

json

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

What I have implemented when I just an a single array.

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




}

parsing call

JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class);
解决方案

You've got an array of arrays of arrays of arrays of Strings. You need

public List<List<List<String[]>>> coordinates = new ArrayList<>();

The following

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;
    }
}

prints

[-71.25, 42.33]

这篇关于Gson Json解析器数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:55
查看更多