2013-03-27 81 views
0

我有一個從API服務這樣一個響應字符串:錯誤解析JSON響應字符串的Android

{"id":"login","status":"true"} 

,這是解析響應字符串從主要獲得價值「狀態」

    JSONObject jsonObj = null; 
    try{ 
     jsonObj = new JSONObject(responseString); 
    } 
    catch(JSONException e){ 
     e.printStackTrace(); 


    } 


     JSONArray innerJsonArray = null; 
     try { 
      innerJsonArray = jsonObj.getJSONArray("status"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     JSONObject jsonObject = null; 
     try { 
      jsonObject = innerJsonArray.getJSONObject(0); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      System.out.println(jsonObject.getString("status")); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
方式

和我有錯誤 「org.json.JSONArray不能被轉換爲JSONObject的」

任何人都可以給我建議?

+0

使用'getBoolean()''不getJSONArray()'... – 2013-03-27 14:37:31

+0

明確指向錯誤您嘗試的JSONObject轉換爲JsonArray。因爲當前字符串只包含的JSONObject – 2013-03-27 14:37:51

回答

0
JSONObject jsonObj = null; 
try{ 
    jsonObj = new JSONObject(responseString); 
    System.out.println(jsonObj.getString("status")); 
} 
catch(JSONException e){ 
    e.printStackTrace(); 
} 

你不需要與任何其他陣列打擾。

+0

徵求意見的感謝,我的作品 – Vinno 2013-03-27 14:55:07

0

在哪一行你會得到異常?在任何可能的方式是你應該使用

jsonObj =JSONObject.fromObject(responseString);

0

請嘗試下面的代碼段代碼。試試這個link獲得更好的解析JSON響應的知識。

JSONObject jObj; 
     try { 
      jObj = new JSONObject(responseString); 
      Log.i("id==", jObj.getString("id")); 
      Log.i("status==", jObj.getString("status")); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }