2013-02-11 55 views
0
String wat = ""; 

     try{ 
       JSONObject responsee = new JSONObject(result); 
       wat = responsee.getString("Result"); 
      } 
      catch(JSONException e){ 
        Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

結果=未值{ 「結果」: 「d」}的Android for的JSONObject

此代碼崩潰應用並拋出以下錯誤:

Error parsing data org.json.JSONException: No value for {"Result":"D"}

+0

你能打印你的回覆嗎? – 2013-02-11 14:14:46

+0

調試器顯示:{「Result」:「D」} – Disa 2013-02-11 14:22:31

+1

您的代碼不符合該例外情況。例外情況表明,當你的代碼顯示你只用'getString'和'Result''時,你將字符串'{「Result」:「D」}'傳遞給某個'get'方法。所以請檢查你的代碼。 – 2013-02-11 14:25:46

回答

1

responsee本身JSON對象你需要。你不需要撥打getJSONObject就可以了。只需使用responsee.getString("Result");即可。下面是一個例子,你可能需要使用getJsonObject

// result = {"name": "John", "age": 10, "father": {"name": "Tom", "age": 30"}} 

JSONObject response = new JSONObject(result); 
JSONObject father = response.getJSONObject("father"); 
String fathersName = father.getString("name"); 
+0

是的,這固定我的代碼。感謝幫助! – Disa 2013-02-11 14:31:28

2

試試這個:

JSon Response like below 
{"data": { "name": "AAA", "Age": "11"}} 

您將使用代碼來獲得以下數據,

JSONObject response = new JSONObject(result); 
    JSONObject json= response.getJSONObject("data"); 
    String Name = json.getString("name"); 
    String Age =json.getString("Age"); 

的迴應是:AAA和11