thepoosh
thepoosh

Reputation: 12587

parse string array from JSON

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

Upvotes: 3

Views: 34179

Answers (3)

thepoosh
thepoosh

Reputation: 12587

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

Upvotes: 12

LiuYan 刘研
LiuYan 刘研

Reputation: 1624

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

Upvotes: 0

Wins
Wins

Reputation: 3460

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

Upvotes: 0

Related Questions