3

i have this JSON object:

{"error":null,
 "result":[{"id":"1234567890",
            "count":1,
            "recipients":
            ["u3848",
               "u8958",
               "u7477474"
            ],
            "dateCreated":"2012-06-13T09:13:45.989Z"
           }]
 }

and I'm trying to find a way to correctly parse the recipients array into a String[] object.

is there an easy way to do this?

EDIT:

found this answer that has all the things needed for result: Sending and Parsing JSON Objects

Community
  • 1
  • 1
thepoosh
  • 12,497
  • 15
  • 73
  • 132

3 Answers3

12

the way to do what I wanted was this:

JSONArray temp = jsonObject.getJSONArray("name");
int length = temp.length();
if (length > 0) {
    String [] recipients = new String [length];
    for (int i = 0; i < length; i++) {
        recipients[i] = temp.getString(i);
    }
}
thepoosh
  • 12,497
  • 15
  • 73
  • 132
0

You can try to use xstream with json serializer. Take a look this link

Wins
  • 3,420
  • 4
  • 36
  • 70
0

I always suggest my favorite library json-lib to handle JSON stuffs.

You can use JSONArray to convert to Object[], although it's not String[], you can still use it because every Object has toString() method.

String sYourJsonString = "['u3848', 'u8958', 'u7477474']";
Object[] arrayReceipients = JSONArray.toArray (JSONArray.fromObject(sYourJsonString));
System.out.println (arrayReceipients [0]); // u3848
LiuYan 刘研
  • 1,614
  • 1
  • 16
  • 29