0

可能重复:
在 android 中读取 Json 数组

我的 json 字符串就像

String jsonString = "[{"NotificationID":115,"TaskID":129,"Sender":"v...@live.com","NotificatonMessage":"dd","NotificationCategory":"missed"}, {"NotificationID":114,"TaskID":129,"Sender":"v...@live.com","NotificatonMessage":"129","NotificationCategory":"opened"},{"NotificationID": 112,"TaskID":129,"Sender":"v...@live.com","NotificatonMessage":"dd","NotificationCategory":"missed"},{"NotificationID":111,"TaskID" :129,"Sender":"d...@hotmail.com","NotificatonMessage":"您已被分配任务 Bydip_shres@hotmail.com","NotificationCategory":"notify"},{"NotificationID":72,"TaskID":125,"Sender":"d...@yahoo.com","NotificatonMessage":"您已被分配任务 Byd.stha1st@ yahoo.com","NotificationCategory":"notify"}]";

我想将其转换为 jsonobject。我在做这个。。

jsonObject = new JSONObject(jsonString);

但它抛出 JSONException ......我怎样才能将这样的字符串转换为 jsonarray ?

4

4 回答 4

1

你得到了一个数组:

JSONArray arr = new JSONArray( jsonString );
于 2013-01-15T06:52:30.330 回答
1

你有一个 JSON 数组而不是一个 JSON 对象。

JSON 对象必须在字符 1 处以 { 开头并以 } 结尾,而 JSONArray 文本必须在字符 1 处以 '[' 开头并以 ] 结尾。

您可以使用获取 JSON 数组

JSONArray obj = new JSONArray(jsonString);
于 2013-01-15T06:55:58.200 回答
1

使用此代码和更多信息JSONParsing请参阅此链接

它会对你有所帮助JSON parsing

 try
    {
        JSONArray jArray = new JSONArray(jsonString);

        for(int i=0;i<jArray.length();i++)
        {
           JSONObject jsonObj = jArray.getJSONObject(i);

           String NotificationID =jsonObj.getString("NotificationID");
           String TaskID=jsonObj.getString("TaskID");
           String Sender=jsonObj.getString("Sender");
           String NotificatonMessage=jsonObj.getString("NotificatonMessage"); 
           String NotificationCategory=jsonObj.getString("NotificationCategory"); 

        }
    }
    catch(JSONException e)
    {
        Log.e("log_tag", "Error parsing data "+e.toString());
    }
于 2013-01-15T06:57:45.107 回答
1

好 ,

you need to extract jsonArray directly by 

    JSONArray array=new JSONArray(jsonString);

然后,您可以使用 for 循环或您想要采用的任何方法迭代数组。

于 2013-01-15T07:12:06.747 回答