2016-08-03 36 views
-1

我怎麼可以在這個JSON循環:Android - Loop on JsonArray?

[{ 
    "X": "54.6000621", 
    "Y": "45.8360411", 
    "Dates": [{ 
     "Date": "2000\/04\/26", 
     "Time": "12:13:45" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "13:13:45" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "14:13:12" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "15:13:10" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "16:13:48" 
    }], 
    "Count": 5, 
    "X": "98.6254621", 
    "Y": "99.8360411", 
    "Dates": [{ 
     "Date": "2012\/04\/26", 
     "Time": "12:13:45" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "13:13:45" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "14:13:12" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "15:13:10" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "16:13:48" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "15:13:10" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "15:13:10" 
    }], 
    "Count": 7, 
    "X": "58.4582621", 
    "Y": "85.8360411", 
    "Dates": [], 
    "Count": 0 
}] 
+1

關注[這](http://www.tutorialspoint.com/ android/android_json_parser.htm) – Sanoop

+0

這甚至不是有效的json格式。它具有重複的鍵名稱。 – sinsuren

+0

使用下面給出的多循環概念 –

回答

2

Yo烏拉圭回合的反應應該是這樣的,看一下反應就像是我的回答

[{ 
    "X": "54.6000621", 
    "Y": "45.8360411", 
    "Dates": [{ 
    "Date": "2000\/04\/26", 
    "Time": "12:13:45" 
    }, { 
    "Date": "2000\/04\/26", 
    "Time": "13:13:45" 
    }, { 
    "Date": "2000\/04\/26", 
    "Time": "14:13:12" 
    }, { 
    "Date": "2000\/04\/26", 
    "Time": "15:13:10" 
    }, { 
    "Date": "2000\/04\/26", 
    "Time": "16:13:48" 
    }], 
    "Count": 5 
}, 
{ 
    "X": "98.6254621", 
    "Y": "99.8360411", 
    "Dates": [{ 
    "Date": "2012\/04\/26", 
    "Time": "12:13:45" 
    }, { 
    "Date": "2012\/04\/26", 
    "Time": "13:13:45" 
    }, { 
    "Date": "2012\/04\/26", 
    "Time": "14:13:12" 
    }, { 
    "Date": "2012\/04\/26", 
    "Time": "15:13:10" 
    }, { 
    "Date": "2012\/04\/26", 
    "Time": "16:13:48" 
    }, { 
    "Date": "2012\/04\/26", 
    "Time": "15:13:10" 
    }, { 
    "Date": "2012\/04\/26", 
    "Time": "15:13:10" 
    }], 
    "Count": 7 
}, 
{ 
    "X": "58.4582621", 
    "Y": "85.8360411", 
    "Dates": [], 
    "Count": 0 

}] 

代碼,以價值

 try { 
     JSONArray jsonArray=new JSONArray(response); 
     for (int i=0;i<jsonArray.length();i++) 
     { 
      JSONObject jsonObject=jsonArray.getJSONObject(i); 

      String X=jsonObject.getString("X"); 
      String Y=jsonObject.getString("Y"); 
      String Count=jsonObject.getString("Count"); 

      JSONArray jsonArraydates=jsonObject.getJSONArray("Dates"); 

      for (int j=0;j<jsonArraydates.length();j++) 
      { 
       JSONObject jsonObjectDates=jsonArraydates.getJSONObject(j); 


       String Date=jsonObjectDates.getString("Date"); 
       String Time=jsonObjectDates.getString("Time"); 


      } 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
0

使用多重循環的概念

try { 
      JSONArray mArray = new JSONArray("your resonce json string"); 
      for (int index = 0; index < mArray.length(); index++) { 
       JSONObject mJsonObject = mArray.getJSONObject(0); 
       JSONArray mDataArray = mJsonObject.getJSONArray("Dates"); 
       for (int row = 0; row < mDataArray.length(); row++) { 
        JSONObject mDatesObject = mDataArray.getJSONObject(0); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
0

有在您發佈的JSON錯誤...

這應該是正確的JSON。 ..

[{ 
    "X": "54.6000621", 
    "Y": "45.8360411", 
    "Dates": [{ 
     "Date": "2000\/04\/26", 
     "Time": "12:13:45" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "13:13:45" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "14:13:12" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "15:13:10" 
    }, { 
     "Date": "2000\/04\/26", 
     "Time": "16:13:48" 
    }], 
    "Count": 5, 
    "X": "98.6254621", 
    "Y": "99.8360411", 
    "Dates": [{ 
     "Date": "2012\/04\/26", 
     "Time": "12:13:45" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "13:13:45" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "14:13:12" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "15:13:10" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "16:13:48" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "15:13:10" 
    }, { 
     "Date": "2012\/04\/26", 
     "Time": "15:13:10" 
    }], 
    "Count": 7, 
    "X": "58.4582621", 
    "Y": "85.8360411", 
    "Dates": [], 
    "Count": 0 
}] 
+0

某些格式將很好,以幫助發現錯誤 – Acapulco