解析JSONObject并在视图中设置它们

时间:2013-04-24 21:45:56

标签: android json

我正在ping服务器JSON响应。这是响应的样子:

{
"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

} }

我正在通知我从服务器接收的内容。我怎样才能将它用于我的目的。

我必须解析“text”get 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);

我想这不是我应该解析的方式。请指导我。

任何形式的帮助都将受到赞赏。

3 个答案:

答案 0 :(得分: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 Bean:

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 String时,只需执行:

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

获取文字:

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

注意:您还可以通过getList().size();

获取notificationCount

答案 1 :(得分: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"));      
}

帮助函数查找ID

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;
    }
}

答案 2 :(得分: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  
  }