Java JSON解析多维数组截取
我在android应用程序中解析JSON时遇到了一个问题。在下面的屏幕截图中,我提供了JSON的结构。我只能得到“几何体”JSONObject。最终,我需要一个数组,其中包含每个“功能”的LatLng 如果有人能给我指出正确的方向,我将非常感激 这是我的密码Java JSON解析多维数组截取,java,json,multidimensional-array,Java,Json,Multidimensional Array,我在android应用程序中解析JSON时遇到了一个问题。在下面的屏幕截图中,我提供了JSON的结构。我只能得到“几何体”JSONObject。最终,我需要一个数组,其中包含每个“功能”的LatLng 如果有人能给我指出正确的方向,我将非常感激 这是我的密码 //gets the area's out of the JSON JsonObjectRequest LosLoopJson = new JsonObjectRequest(losloopURL, new Response.Li
//gets the area's out of the JSON
JsonObjectRequest LosLoopJson = new JsonObjectRequest(losloopURL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray features = response.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
JSONObject Area = features.getJSONObject(i);
for (int x = 0; x < features.length(); x++) {
JSONObject geometry = Area.getJSONObject("geometry");
Double[][][] coordinates = geometry.get("coordinates");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
requestQueue.add(objectrequest);
//从JSON中获取区域的
JsonObjectRequest LosLoopJson=新的JsonObjectRequest(losloopURL,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray features=response.getJSONArray(“features”);
对于(int i=0;i
这是我的JSON结构:
提前谢谢 坐标是一个json数组,其中的每个元素也包含一个json数组。所以你必须迭代坐标对象
JSONArray coordinates = geometry.get("coordinates");
for(int i = 0; i < coordinates.size(); i++)
JSONArray coord = coordinates.get(i);
JSONArray坐标=geometry.get(“坐标”);
对于(int i=0;i
试试这个:-
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("features");
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (jsonObject1.has("geomerty")){
JSONArray jsonArray1 = jsonObject1.getJSONArray("geomerty");
for (int j = 0; j <= jsonArray1.length(); j++){
if (jsonObject1.has("coordinates")){
JSONArray jsonArray2 = jsonObject1.getJSONArray("coordinates");
for (int k = 0; k <= jsonArray2.length();k++){
JSONArray jsonArray3 = jsonArray2.getJSONArray(k);
for (int l =0; l <= jsonArray3.length(); l++){
Log.d("ABC", String.valueOf(jsonArray3.getDouble(l)));
}
}
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray JSONArray=jsonObject.getJSONArray(“特性”);
对于(int i=0;i感谢所有的帮助,我使用以下代码使其工作:
//gets the area's out of the JSON
JsonObjectRequest LosLoopJson = new JsonObjectRequest(losloopURL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray features = response.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
JSONObject Area = features.getJSONObject(i);
JSONObject geometry = Area.getJSONObject("geometry");
JSONArray coordinates = geometry.getJSONArray("coordinates");
JSONArray AreaCoordinates = coordinates.getJSONArray(0);
//Log.d("Debug", "Feature: " + String.valueOf(i + 1));
for (int y = 0; y < AreaCoordinates.length(); y++) {
//Log.d("Debug", "Coordinate: " + String.valueOf(y + 1));
JSONArray DoubleCoordinate = AreaCoordinates.getJSONArray(y);
Double latitude = DoubleCoordinate.getDouble(1);
Double longitude = DoubleCoordinate.getDouble(0);
LatLng coordinate = new LatLng(latitude, longitude);
LosLoopGebied.add(coordinate);
}
losloopgebieden.add(LosLoopGebied);
LosLoopGebied.clear();
}
//PlaatsLosLoopGebieden();
//Log.d("Debug", "Heeft de losloopgebieden opgehaalt uit JSON");
//Log.d("Debug", "Er zijn " + String.valueOf(LosLoopGebied.size()) + " coordinaten opgehaalt");
Log.d("Debug", "Er zijn " + String.valueOf(losloopgebieden.size()) + " opgehaalt uit JSON");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
requestQueue.add(LosLoopJson);
//从JSON中获取区域的
JsonObjectRequest LosLoopJson=新的JsonObjectRequest(losloopURL,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray features=response.getJSONArray(“features”);
对于(int i=0;i
Post code,而不是图片。另外提示:用英语而不是荷兰语、变量名、注释等做任何事情。请将您的代码作为文本而不是屏幕截图发布。这样,其他用户可以复制您的代码并自己尝试。至于您的问题,您可以制作JSONArray
类型的坐标,并在LatLo上循环ngs