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");
解决方案
只需迭代JSONArray
并获取JSONObject
,这又是同一件事,coordinates
从每个获取数组并从中JSONObject
获取值JSONArray
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);
}
}
推荐阅读
- python - 在 pytest 跳过条件中使用命令行选项
- angular - Angular 6 代码覆盖率...如何在应用程序的代码覆盖率中排除本地库?
- javascript - html() 似乎不适用于 li 内的 span 元素
- hive - 分区的配置单元错误
- python-3.x - Keras 中的多个参数化指标
- android - 从 Android 应用程序开发的角度来看,启用对 Samsung DeX 和 Chrome OS 的支持有什么区别?
- reactjs - 使用 i18next-browser-languagedetector 为 reactJs Web 应用程序检测浏览器的语言
- java - java - 关闭单击带有硒的firefox窗口的X时关闭geckodriver?
- java - 我应该将没有任何实现的接口移动到库中吗
- widget - Instagram Widget Sizing - 图片大小不同