I'm trying to simply parse a JSON array with the Java JSON library from json.org. The JSON I retrieve from the server is a JSON array and has the following structure:
[
{
"a": "u",
"b": "v"
},
{
"a": "x",
"b": "y"
}
]
To parse it, I use this simple instruction:
JSONArray jsonArray = new JSONArray(result);
for(int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//further processing here
}
This should work fine, but I get this error on execution:
JSONArray[0] is not a JSONObject.
In an attempt to solve this, I printed the JSONArray object using its toString() implementation and I get this:
["[{"a":"u","b":"v"},{"a":"x","b":"y"}]"]
So strangely enough, the JSONArray(String string) method adds [""]
around the provided JSON... Hence the previous error, as the first child of the array is a String. How could I solve this?
Note that I use the latest version of the json.org library (November 13th 2014). I used it before in Android applications and I never met this problem. If possible, I'd like not to use another library.