Take the 2-minute tour ×

I have a long JSON string representing a string[] array being returned from a WCF service. The array elements are simply strings, they are not objects. This is an example of the return data


I don't mind the index being included in the string, but I need to parse the strings into an ArrayList in Android so that I can adapt it to a ListView.

Since these technically aren't JSONObjects, how can I iterate over them and extract the string from each array element?

share|improve this question
I'm a little confused. Is the WCF returning a string formatted as a JSON array or an actual array of strings? If it's an array why don't you just iterate through each array element, strip out the index and run a JSON deserializing function on the resulting string? –  Spencer Ruport Apr 13 '12 at 20:39
add comment

3 Answers

up vote 5 down vote accepted

this is a valid JSON array of strings, you can parse it normally like this

JSONArray jsonStrings = json.getJSONArray("items");
String strings[] = new String[jsonStrings.length()];
strings[i] = jsonStrings.getString(i);
share|improve this answer
Yeah, this is it, shortly after posting this I realized I made this more difficult than it needed to be. –  SmashCode Apr 13 '12 at 21:03
add comment

Maybe this post can help you out:

JSON Array iteration in Android/Java

        applicationSettings.put(name, value);

 JSONArray names= json.names();
    JSONArray values = json.toJSONArray(names);
            String dateString = values.getString(i);
            String dateString = values.getString(i);
share|improve this answer
Not really, I've had to do something similar to what you posted in other Android projects, but that was dealing with JSONArrays that contained objects. If the data being returned was the format ["Name" : "the_name", ... ] I'd be fine, but if you look at the data being returned, your code wouldn't work. –  SmashCode Apr 13 '12 at 20:35
Thats true, the post below could be more what your looking for. –  Rob Angelier Apr 13 '12 at 20:43
add comment

Just to clarify, as I understand it you are receiving a JSON array of strings which are strings of valid JSON prefixed with the array index and a pipe character?

First, I would suggest contacting the creator of this abomination and lecturing them on violating standards. If they won't listen, talk to their bosses. This kind of thing is unacceptable in my opinion and needs to stop.

Second, I'd suggest doing something like this:

string[] element_strings = JSON.deserialize(WCF_string_result);
object[] elements = new object[element_strings.length];
    int pipeIndex = element_strings.indexOf("|");
    elements[x] = JSON.deserialize(element_strings[x].substr(pipeIndex + 1));

Of course this assumes you have some kind of method for deserializing strings of JSON objects. If you don't I'd recommend using the built in library available in Android:


share|improve this answer
add comment

Your Answer


