Reputation:
I want to convert a Json string to an Array here is my current code
String[] comments = json2.getString(KEY_COMMENT);
KEY_COMMENT is a string which contains multiple comments. The comments were gathered in a php array then sent back to the phone into a Json string. How can I convert the string to an array?
An example of what the comments look like is this
07-08 20:33:08.227: E/JSON(22615): {
"tag":"collectComments",
"success":1,
"error":0,
"numberOfComments":16,
"offsetNumber":1,
"comment":["test 16",
"test 15",
"test 14",
"test 13",
"test 12",
"test 11",
"test 10",
"test 9",
"test 8",
"test 7",
"test 6",
"test 5",
"test 4",
"test 3",
"test 2",
"test 1"]}n
Upvotes: 0
Views: 1590
Reputation: 2440
To use the default JSON lib try this:
// convert from json string to something useable JSONArray commentsArray = json2.JsonArray(KEY_COMMENT);
// loop through the elements int length = commentsArray.length() for (int index = 0; index < length; index++) { String comment = commentsArray.getString(index); System.out.println(comment); }
For more information, I recomment this tutorial http://www.vogella.com/articles/AndroidJSON/
Upvotes: 0
Reputation: 3908
It looks like you're using the org.json library and that you have already called the public JSONObject(java.lang.String source)
string-based constructor to parse the full string into a local var named json2
, presumably with something like this:
String json = ... // "{\"tag\":\"collectComments\",\"success\":1,...
JSONObject json2 = new JSONObject(json);
String KEY_COMMENT = "comment";
but instead of String[] comments = json2.getString(KEY_COMMENT);
(which tries to get the comments as a string; something that probably shouldn't work since it's an array, but does) you probably actually want:
JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
At this point, commentArray
is a JSONArray, which can be a heterogeneous list of values, including strings, numbers, booleans, nulls, and even arrays. If you want to turn this into an actual Java array of String, you'll need to walk it, converting as you go:
String comments[] = new String[commentArray.length()];
for ( int i=0; i<commentArray.length(); i++ ) {
comments[i] = commentArray.getString(i);
}
Upvotes: 1
Reputation: 3739
I found GSON a great tool to work with json objects on Android. It's a java library to convert JSON to Java object.
You can check it out here: https://sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples
Here's their example working with Arrays:
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
// Serialization
gson.toJson(ints); ==> prints [1,2,3,4,5]
gson.toJson(strings); ==> prints ["abc", "def", "ghi"]
// Deserialization
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
In your case, you would set up the class as follow:
public class Comment {
public String tag;
public String[] comments;
// define other elements (numberOffset, error...) if needed
}
Then, to de-serialize the json response, in your code:
Comment item = gson.fromJson(jsonString, Comment.class);
// Access the comments by
String[] comments = item.comments;
// Access the tag
String tag = item.tag;
Upvotes: 0