2011-11-28 71 views
5

我有一個JSON格式像這樣Android的JSON對象

{ 「迴應」:{ 「地位」:真正的 「結果」: 「user_exists」}}

我現在想檢索狀態值做一些邏輯

JSONObject jData = new JSONObject(data); 
JSONArray response = jData.getJSONArray("response"); 

但我收到以下錯誤

org.json.JSONException:值{ 「結果」: 「user_exists」, 「狀態」:真} 在類型org.json.JSONObject的響應不能被轉換到 JSONArray

如何檢索一個從內部和對象的對象?

回答

4

您嘗試中檢索從JSONArray狀態attribut但是,你沒有任何JSONArray在你的代碼中,(JSONArray[]包圍,而JSONObject{}包圍), 所以中檢索狀態值,試試這個:

JSONObject jData = new JSONObject(data); 
JSONObject response = jData.getJSONObject("response"); 

boolean status = response.getBoolean("status"); 
+0

感謝很多的代碼示例:) –

+0

歡迎你:),我們來這裏是爲了幫助 – Houcine

1

你必須通過

JSONObject response = jData.getJSONObject("response")代替JSONArray首先導航到響應對象,作爲響應是一個對象。

1

response不是一個數組,而是一個對象。使用getJSONObjectJSONObject而不是getJSONArrayJSONArray

5

response是一個JSONObject,而不是JSONArray。數組對象被這些[]括號包圍,對象使用正常的對象{}(更多格式的信息,請參閱json.org

變化

JSONArray response = jData.getJSONArray("response"); 

JSONObject response = jData.getJSONObject("response");