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
asked Jun 13, 2012 at 9:33
thepoosh
12.6k15 gold badges77 silver badges132 bronze badges
3 Answers 3
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);
}
}
answered Jun 26, 2012 at 7:19
thepoosh
12.6k15 gold badges77 silver badges132 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Mathias Conradt
Where is that "name" attribute coming from? You don't have that anywhere in your original post.
thepoosh
the
name attribute needs to be predefined since JSON is a key/value store.Mathias Conradt
Right. (I was wondering because you replied to your own question, where the key was not there.) Should be "recipients" instead of "name" therefore.
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
answered Jun 13, 2012 at 10:03
LiuYan 刘研
1,6241 gold badge16 silver badges31 bronze badges
Comments
lang-java
org.json.JSONObjectlibrary