2016-11-26 28 views
1

我一直在從具有多個數據集的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?

+1

響應不是正確的JSON格式 – Nithinlal

+0

@Nithinlal我正要指向相同的 –

回答

2

你要取你的JSONArray這是一個JSONObject裏面,所以創建一個JSONObject和索引抓取您的陣列「上崗」

1)resultJSONObject所以創建JSONObject

2。 )通過指數獲取它與索引值取你的JSONArray爲「上崗」

3)現在只需遍歷數組對象

 JSONObject jObj = new JSONObject(result); 
     JSONArray jArray = jObj.getJSONArray("posts"); 

     // 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); 
     } 

注:我不知道天氣是用較短的版本,雖然你的JSON對象的樣品的反應應該與},結束。

[{ 「ID」:20038, 「類型」: 「後」, 「彈頭」: 「XPERIA-發射器下載」, 「URL」:「http://missingtricks.net/xperia-launcher -download/「 」狀態「: 」發佈「, 」稱號「: 」下載 的Xperia桌面應用的Android(最新版)「,

// ^^^ there should be a } not a , to end json 
// so make sure to do the correction so it will look like => ...st Version)"}, 

{ 」ID「:94」輸入「:」後「‘彈頭’:‘免費頂級呼 - 應用 - 的2014年’,‘URL’:」 http://missingtricks.net/top-free-calling-apps-of- 2014年/「,」狀態「:」發佈「,」標題「:」最佳 免費撥打Android版2014年11月「,]

改進:

可以使用optString避免空或非字符串值,如果沒有映射關鍵

這有兩個變化

獲取一個可選的字符串與一個關鍵字相關聯。如果沒有這樣的密鑰,它將返回 defaultValue。

public String optString(String key, String defaultValue) { 
fishData.status = json_data.optString("status","N/A"); 
// will return "N/A" if no key found 

如果沒有找到鍵,然後簡單地使用

fishData.status = json_data.optString("status"); 
// will return "" if no key found where "" is an empty string 
+1

使用optString而不是getString它將避免零點異常 – Nithinlal

+0

@Nithinlal是啊這是我給大家的建議,我只是看對於一箇舊的帖子指出這一點:)謝謝 –

+1

這是工作! –

1

您可以驗證您的JSON here要得到空字符串。

如果整個JSON getJsonObject()不起作用,那麼你應該解析JSON對象&數組,然後使用它們。