-1

我正在 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并将其粘贴到视图上。我做错了。这是我所做的:

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

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

任何形式的帮助将不胜感激。

4

3 回答 3

1

你听说过格森吗?它可以帮助您将 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 字符串时,只需执行以下操作:

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

要获取文本:

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

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

于 2013-04-24T22:22:41.587 回答
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;
    }
}
于 2013-04-24T22:12:53.523 回答
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  
  }
于 2013-04-24T22:17:02.780 回答