Android解析嵌套jsonArray



我正在与android一起工作。我想解析一个嵌套的json数组没有名字。我如何解析这些数组并将它们存储到一个列表中,我需要将这些列表创建一个列表。如何解析这些数组?提前谢谢。

[
    [
        [
            72.837,
            18.956,
            0
        ],
        [
            72.817,
            18.956,
            0
        ],
        [
            72.8095,
            18.956,
            0
        ],
        [
            72.8082,
            18.956,
            0
        ],
        [
            72.808,
            18.956,
            0
        ],
        [
            72.8076,
            18.9562,
            0
        ],
        [
            72.8072,
            18.9564,
            0
        ],
        [
            72.8071,
            18.9578,
            0
        ],
        [
            72.807,
            18.9617,
            0
        ],
        [
            72.807,
            18.9757,
            0
        ],
        [
            72.8072,
            18.9767,
            0
        ],
        [
            72.8072,
            18.9768,
            0
        ],
        [
            72.8074,
            18.9774,
            0
        ],
        [
            72.8088,
            18.9777,
            0
        ],
        [
            72.8112,
            18.978,
            0
        ],
        [
            72.8414,
            18.9779,
            0
        ],
        [
            72.8438,
            18.9778,
            0
        ],
        [
            72.8439,
            18.9773,
            0
        ],
        [
            72.8439,
            18.9606,
            0
        ],
        [
            72.8439,
            18.9583,
            0
        ],
        [
            72.8428,
            18.9566,
            0
        ],
        [
            72.8423,
            18.9562,
            0
        ],
        [
            72.8403,
            18.9561,
            0
        ],
        [
            72.8376,
            18.956,
            0
        ],
        [
            72.837,
            18.956,
            0
        ]
    ]
]

你可以做

JSONArray array = new JSONArray(response); //Where response is your json string

然后你可以使用for循环迭代抛出这个数组

for(int i=0; i<array.length(); i++) {
    //Do something using :
    //array.get(i) or
    //array.getJsonArray(i) or
    //array.getDouble(i) or
    //...
}

下面是JsonArray

的文档

EDIT如果你想将嵌套的jsonArray转换成嵌套的java列表,你可以这样做(未测试)

List<List<Double>> arrayList = new ArrayList<>();
JSONArray jsonArray= new JSONArray(response).getJSONArray(0);
for(int i=0; i<jsonArray.length(); i++) {
    JsonArray nestedArray = jsonArray.get(i);
    List<Double> nestedList = new ArrayList<>();
    for(int j=0; j< nestedArray.length(); j++) {
        nestedList.add(nestedArray.getDouble(j));
    }
    arrayList.add(nestedList);
}

使用Gson和这样的类来解析您的array of arrays of arrays:

public class Example extends ArrayList<ArrayList<ArrayList<Double>>> {}

最新更新