我一直在從具有多個數據集的JSON文件中獲取數據。如何訪問具有多個數組對象的JSON數據:android
{"status":"ok","count":3,"count_total":661,"pages":133,"posts":
[{"id":20038,"type":"post","slug":"xperia-launcher-download","url":"http:\/\/missingtricks.net\/xperia-launcher-download\/","status":"publish","title":"Download Xperia Launcher app for Android (Latest Version)",
{"id":94,"type":"post","slug":"top-free-calling-apps-of-2014-year","url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2014-year\/","status":"publish","title":"Best Free Calling Apps for Android November 2014",
{"id":98,"type":"post","slug":"top-free-calling-apps-of-2016-year" "url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2016-year\/","status":"publish","title":"Best Free Calling Apps for Android December 2016"}]}
我需要從上面的JSON文件訪問標題,URL和狀態。
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<DataFish> data = new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
DataFish fishData = new DataFish();
fishData.status = json_data.getString("status");
fishData.title = json_data.getString("url");
fishData.sizeName = json_data.getString("title");
data.add(fishData);
}
} catch (JSONException e) {
Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
Log.d("Json","Exception = "+e.toString());
}
}
我得到一個JSONException與上面的代碼。
如何從JSON文件訪問標題,狀態和URL?
響應不是正確的JSON格式 – Nithinlal
@Nithinlal我正要指向相同的 –