如何解析数组中的多个JSON对象?

时间:2019-01-20 17:59:20

标签: java json

我正在尝试从JSONObjects中的“功能”数组中保存每个坐标数组,但是随着对象的不断更新,我必须找到一种自动方法来解析所有对象并将其坐标保存到谷歌地图点。 (我想通过Java解析)

JSON文件(它会更新,因此看起来会有所不同,这就是为什么我要寻找一种自动方式,我想将它们保存到MapsActivity中的LatLng中)

{
"type": "FeatureCollection",
"features": [

{
"geometry": {
"type": "Point",
"coordinates": [
109198.840917,
100501.626971
]
},
"type": "Feature",
"id": "cortes_de_transito|71766",
"properties": {
"Motivo": "Obra",
"Tipo": ""
}
},

{
"geometry": {
"type": "Point",
"coordinates": [
102563.558027,
94833.763929
]
},
"type": "Feature",
"id": "cortes_de_transito|71785",
"properties": {
"Motivo": "Obra",
"Tipo": ""
}
},

{
"geometry": {
"type": "Point",
"coordinates": [
107047.021566,
102659.117425
]
},
"type": "Feature",
"id": "cortes_de_transito|74195",
"properties": {
"Motivo": "",
"Tipo": ""
}
}

]
}

和Java代码,我刚刚解析了“功能”数组

JSONArray main_array = response.getJSONArray("features");

1 个答案:

答案 0 :(得分:0)

只需迭代JSONArray并获取JSONObject,这同样是从每个coordinates获取JSONObject数组,并从该JSONArray获取值/ p>

for(int i=0; i<array.length(); i++) {
        JSONObject obj = array.getJSONObject(i);

        JSONArray ar = obj.getJSONArray("coordinates");

        for(int j=0; j<ar.length(); j++) {
            String co = ar.getString(i);
            System.out.println(co);
        }
    }