2013-04-24 44 views
-1

我正在ping服務器的JSON響應。這裏是什麼樣子的響應:解析JSONObject並將它們設置在視圖中

{ 
"notifications":{ 
    "0":{ 
    "id":"17546", 
    "uID":"6698", 
    "text":"Earned 22 points for searching", 
    }, 
    "1":{ 
    "id":"17545", 
    "uID":"6698", 
    "text":"Earned 1 point for searching", 
    }, 
    "2":{ 
    "id":"17544", 
    "uID":"6698", 
    "text":"Earned 1 point for searching", 
    }, 
    "3":{ 
    "id":"17543", 
    "uID":"6698", 
    "text":"Earned 1 point for searching", 
    }, 
    "4":{ 
    "id":"17528", 
    "uID":"6698", 
    "text":"Earned 1 point for searching", 
    }, 
    "notificationCount":5 

}}

我得到什麼,我從服務器receving總通知。我如何使用它來達到我的目的。

我必須解析「文本」得到text並粘貼在視圖上。我做錯了。這是我所做的:

JSONObject jNotification0 = jSearchData.getJSONObject("0"); 
JSONObject jNotification1 = jSearchData.getJSONObject("1"); 
JSONObject jNotification2 = jSearchData.getJSONObject("2"); 
JSONObject jNotification3 = jSearchData.getJSONObject("3"); 
JSONObject jNotification4 = jSearchData.getJSONObject("4"); 

String jText0 = jNotification0.getString("text"); 
String jText1 = jNotification1.getString("text"); 
String jText2 = jNotification2.getString("text"); 
String jText3 = jNotification3.getString("text"); 
String jText4 = jNotification4.getString("text"); 

TextView notificationText1 = (TextView) dialogLoggedInNotification.findViewById(R.id.notificationText1); 
TextView notificationText2 = (TextView) dialogLoggedInNotification.findViewById(R.id.notificationText2); 
TextView notificationText3 = (TextView) dialogLoggedInNotification.findViewById(R.id.notificationText3); 
TextView notificationText4 = (TextView) dialogLoggedInNotification.findViewById(R.id.notificationText4); 
TextView notificationText5 = (TextView) dialogLoggedInNotification.findViewById(R.id.notificationText5); 

notificationText1.setText(jText0); 
notificationText2.setText(jText1); 
notificationText3.setText(jText2); 
notificationText4.setText(jText3); 
notificationText5.setText(jText4); 

我想這不是我應該做的解析方式。請在此引導我。

任何形式的幫助將不勝感激。

+0

您確定通知是在大括號中,而不是在正常大括號中嗎?正常的支撐將表明一個Json數組,這應該使解析變得非常簡單! – Lefteris 2013-04-24 22:22:32

+0

你是什麼意思「以錯誤的方式行事」?這是什麼問題?錯誤,意想不到的結果? – alkis 2013-04-24 22:33:53

+0

@alkis我只有一個通知存在,然後我無法修復它們在視圖上。如果我有4個通知,那麼它運行得很好。 – Anupam 2013-04-24 22:41:41

回答

0

如果您jSearchData是正確的對象,然後這應該工作

String text = jSearchData.getJSONObject("0").getString("text"); 

您可以創建這樣的循環。此外,你的迴應有一個逗號, 是不需要的。

"0":{ 
    "id":"17546", 
    "uID":"6698", 
    "text":"Earned 22 points for searching"**,** //<-- This comma should not exist 
    } 
0

只需遍歷結果並設置文本即可。

for (int i = 0; i<totalNotification; i++) 
{ 
    TextView notificationText = (TextView) dialogLoggedInNotification.findViewById(findId(i)); 
    notificationText.setText((String)jSearchData.getJSONObject(Integer.toString(i)).get("text"));  
} 

助手功能找到IDS

public int findId(int idIn) 
{ 
    switch (idIn) 
    { 
     case 1: 
      return R.id.notificationText1; 
     case 2: 
      return R.id.notificationText2; 
     case 3: 
      return R.id.notificationText3; 
     case 4: 
      return R.id.notificationText4; 
     case 5: 
      return R.id.notificationText5; 
    } 
} 
+0

請參閱我的編輯,我已經添加了代碼,我如何在textviews中設置文本。你能幫助我嗎? – Anupam 2013-04-24 22:22:34

+0

已更新爲新代碼 – zimmryan 2013-04-24 22:49:19

1

您是否聽說過Gson?它可以幫助您將JSON對象解析爲Java對象。你的JSON數據雖然有點奇怪。爲什麼響應的通知無法存儲在數組中,而是存儲在單獨的JSON對象中?

我的意思是,爲什麼不:

{ 
    "notification": [ 
     { 
       "id":"17546", 
       "uID":"6698", 
       "text":"Earned 22 points for searching", 
     }, 
     { 
       "id":"17545", 
       "uID":"6698", 
       "text":"Earned 1 point for searching", 
     }, 
     { 
       "id":"17544", 
       "uID":"6698", 
       "text":"Earned 1 point for searching", 
     }, 
     { 
       "id":"17543", 
       "uID":"6698", 
       "text":"Earned 1 point for searching", 
     }, 
     { 
       "id":"17528", 
       "uID":"6698", 
       "text":"Earned 1 point for searching", 
     } 
    ], 
    "notificationCount": 5 
} 

如果你能有這樣的事情,GSON是非常容易使用。創建這些Java豆類:

public class JsonObj { 
    private List<Notification> list; 
    private int notificationCount; 

    public JsonObj { } 

    public JsonObj(List<Notification> list, int notificationCount) {...} 

    public List<Notification> getList() {...} 

    .... get/set methods 
} 

public class Notification { 
    private Long id; 
    private Long uID; 
    private String text; 

    public Notification { } 

    public Notification(Long id, Long uId, String text) {...} 

    public Long getId() {...} 

    .... get/set methods 
} 

然後,當你得到JSON字符串,只是做:

JsonObj obj = new Gson().fromJson(jsonData, JsonObj.class); 

來獲取文本:

String txt = obj.getList().get(index).getText(); 

注意:您還可以通過獲得notificationCount getList().size();