0

Possible Duplicate:
Reading a Json Array in android

my json string is like

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":"You have been Assigned Task Bydip_shres@hotmail.com","NotificationCategory":"notify"},{"NotificationID":72,"TaskID":125,"Sender":"d...@yahoo.com","NotificatonMessage":"You have been Assigned Task Byd.stha1st@yahoo.com","NotificationCategory":"notify"}]";

I want to convert it to jsonobject. I am doing this..

jsonObject = new JSONObject(jsonString);

but its throwing JSONException... how can i convert such string to jsonarray ?

Community
  • 1
  • 1
magicgb
  • 60
  • 7

4 Answers4

1

You got yourself an array:

JSONArray arr = new JSONArray( jsonString );
323go
  • 14,070
  • 6
  • 32
  • 40
1

You have got a JSON Array instead of a JSON Object.

JSON Object must start with a { at character 1 and ends with a } wheras a JSONArray text must start with '[' at character 1 and ends with a ].

You can get the JSON ARray using

JSONArray obj = new JSONArray(jsonString);
Rahul
  • 15,264
  • 3
  • 40
  • 61
1

use this code & for more JSONParsing Refer this Link

it will help to you 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());
    }
Dixit Patel
  • 6,179
  • 1
  • 31
  • 42
1

Well ,

you need to extract jsonArray directly by 

    JSONArray array=new JSONArray(jsonString);

Then you can iterate over an array using for loop or whichever u want to adopt.

shubham jain
  • 166
  • 1
  • 9