2012-11-02 43 views
0
{ 
    "status": "ERROR", 
    "msg_content": "Your old password was entered incorrectly. Please enter it again.", 
    "code": "400", 
    "msg_title": "Sorry. Error in processing your request" 
} 

if (str.startsWith("Bad Request")) 
{ 
    textview.setText(" ");       
} 

如何TextView的內部打印使用JSON如何在textview android json中打印msg_content?

回答

2

您需要使用JSON對象解析JSON。

JSONObject obj = new JSONObject(str); 

然後找到任何你想要的JSON對象的字符串。

if (obj.has("msg_content")) { 
     String value = obj.getString("msg_content"); 
     textview.settext(value); 
} 
0

我是很新的JSON顯示msg_content,但我會創造一個JsonReader和解析JSON按here

1

JsonReader在API實現11.如果您想在GingerBread或更低版本中使用它,請嘗試使用this

2

您需要創建JSONObject並將其作爲參數傳遞給字符串。

JSONObject obj = new JSONObject(str); 

然後找到在JSONObject的關鍵只是打電話,經常檢查是否有JSONObject的嘗試檢索之前,這把鑰匙。

if (obj.has("status"){ 
    String status = obj.getString("status"); 
} 

if (obj.has("msg_content"){ 
    String content = obj.getString("msg_content"); 
    textview.setText(content); 
} 
+0

謝謝,比我的JsonReader建議更好。 – PJL

1

使用下面的代碼從JSON對象中提取信息:

try {   
      Iterator keys = jsonObject.keys(); 

      while (keys.hasNext()) { 
       String key = (String) keys.next(); 
       if(key.equals("msg_content")) 
        textView.setText(jsonObject.getString(key)); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

而且,如果u有JSON字符串,美國可以使用下面的代碼填充的對象:

try { 
     jsonObject = new JSONObject(theJsonString); 
    } catch (JSONException e1) { 
     e1.printStackTrace(); 
    }