2012-03-19 121 views
0

我有一個名爲JSONParse的基於JsonObject的類。有此方法:Json對象爲null

public String getName() 
    { 
     try { 
      JSONObject jobj = this.getJSONObject("Data"); 
      Log.e("NFF NAME", jobj.toString()); 
      JSONObject jobj2= jobj.getJSONObject("User"); 
      Log.e("NFF NAME", jobj2.toString()); 
      return jobj2.getString("username"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return ""; 
    } 

日誌:

03-19 19:50:09.280: E/NFF NAME(5909): {"User":{"picture":"http://vm19.htl-leonding.ac.at/img/30.png","gender":"f","username":"lisa","age":32}} 
03-19 19:50:09.280: E/NFF NAME(5909): {"picture":"http://vm19.htl-leonding.ac.at/img/30.png","gender":"f","username":"lisa","age":32} 

問題:

就永遠這個方法的名稱返回null。

請幫助

回答

0

用戶名內部用戶嘗試這種

public String getName() 
    { 
     try { 
      JSONObject jobj = this.getJSONObject("Data"); 
      Log.e("NFF NAME", jobj.toString()); 
      JSONObject jobj2= jobj.getJSONObject("User"); 
      Log.e("NFF NAME", jobj2.toString()); 
      JSONObject jobj2b= jobj2.getJSONObject("User"); 
      Log.e("NFF NAME", jobj2b.toString()); 
      return jobj2b.getString("username"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return ""; 
    } 

我不測試,但您可以嘗試...

EDITED: 我是對不起,我寫得很快,不看代碼,jobj2b指向jobj2第一個孩子:'User'

+0

jobj2和jobj2b變量有什麼區別? – lexmiir 2012-03-19 20:06:34

+0

不起作用...... – user547995 2012-03-19 20:07:18

+0

job2和job2b指向同一節點 – 2012-03-19 20:11:09