2016-07-27 79 views
-2

我得到這樣的迴應:訪問JSON數組內的另一個JSON數組

{ 
     "status": "Success", 
     "data": [{ 
      "careTypeId": "10", 
      "careTypeName": "Vacation Care", 
      "daysOfinterest": ["Monday", "Tuesday"], 
      "childDaysOfInterestId": "212" 
     }, { 
      "careTypeId": "10", 
      "careTypeName": "Vacation Care", 
      "daysOfinterest": ["Monday", "Tuesday", "Thursday"], 
      "childDaysOfInterestId": "202" 
     }], 
     "message": "ChildDaysOf Interest" 
    } 

在這種反應我需要訪問的數據陣列,並從我需要得到daysOfInterest陣列。

+0

它看起來像一個字符串數組,而不是json數組。向我們展示你的模型班。 – SripadRaj

回答

0

首先得到像 jj數據陣列是您的JSON對象

JSONArray RecordList = new JSONArray(jj.getString("data")); 
for (int i = 0; i < RecordList.length(); i++) { 
         JSONObject list = RecordList.getJSONObject(i); 
         JSONArray RecordList1 = new JSONArray(list.getstring("daysOfinterest")); 
Log.e("Test" , "get Result" + RecordList1); 
    } 
} 
0

您可以使用org.json圖書館,閱讀更多關於它here

首先,你必須jsonString轉換爲JSONObject的,然後從JSONObject獲取數據數組,然後您必須在該數組上循環才能獲取daysOfInterest數組。

示例代碼:

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class Example { 

    public static void main(String[] args) { 
     String jsonString = "{\n\"status\": \"Success\",\n\"data\": [{\n\"careTypeId\": \"10\",\n\"careTypeName\": \"Vacation Care\",\n\"daysOfinterest\": [\"Monday\", \"Tuesday\"],\n\"childDaysOfInterestId\": \"212\"\n}, {\n\"careTypeId\": \"10\",\n\"careTypeName\": \"Vacation Care\",\n\"daysOfinterest\": [\"Monday\", \"Tuesday\", \"Thursday\"],\n\"childDaysOfInterestId\": \"202\"\n }],\n\"message\": \"ChildDaysOf Interest\"\n }"; 
     try { 
      JSONObject mainJsonObject = new JSONObject(jsonString); 
      JSONArray dataArray = mainJsonObject.getJSONArray("data"); 
      for (int i = 0; i < dataArray.length(); i++) { 
       JSONObject jsonObject = dataArray.getJSONObject(i); 
       JSONArray daysOfInterestArray = jsonObject.getJSONArray("daysOfinterest"); 
       for (int j = 0; j < daysOfInterestArray.length(); j++) { 
        System.out.println("Days of interest : " + daysOfInterestArray.get(j)); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

輸出:利益
日:週一
天的利息:週二
天的利息爲:週一
天的利息:週二的利益
天數:星期四