Android解析嵌套的jsonArray

时间:2015-10-21 07:41:15

标签: android json

我正在使用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
        ]
    ]
]

2 个答案:

答案 0 :(得分:1)

你可以做到

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

的文档

编辑如果要将嵌套的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);
}

答案 1 :(得分:1)

Gson与此类用于解析array of arrays of arrays

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