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

asked Jun 13, 2012 at 9:33
3
  • Which Json(Java) library are you using? Commented Jun 13, 2012 at 9:41
  • I'm using the org.json.JSONObject library Commented Jun 13, 2012 at 9:42
  • possible duplicate of Sending and Parsing JSON in Android Commented Jun 13, 2012 at 10:16

3 Answers 3

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);
 }
}
answered Jun 26, 2012 at 7:19
Sign up to request clarification or add additional context in comments.

3 Comments

Where is that "name" attribute coming from? You don't have that anywhere in your original post.
the name attribute needs to be predefined since JSON is a key/value store.
Right. (I was wondering because you replied to your own question, where the key was not there.) Should be "recipients" instead of "name" therefore.
0

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

answered Jun 13, 2012 at 9:44

Comments

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
answered Jun 13, 2012 at 10:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.