I need to read this JSON array :
[{
"1":{"id":"0","name":"first"},
"2":{"id":"1","name":"second"}
}]
I use this code
JSONObject jsonObject = null;
for(int i=0; i < array.length(); i++)
{
try
{
jsonObject = array.getJSONObject(i);
System.out.println("Data: " + jsonObject.getString("id"));
}
catch (JSONException e)
{
Log.e("Get Data from JSON Array ..", "Error: " + e.getMessage());
}
}
But the array length = 1 always and the error appear : no value for id
The method to return JSON Array ..
public static JSONArray getJSONArrayFromUrl(String url)
{
String str = "{\"Array\":["; // I add it because the JSON return from the site not contains root ..
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("url);
try
{
response = myClient.execute(myConnection);
str += EntityUtils.toString(response.getEntity(), "iso-8859-1") + "]}";
}
catch (ClientProtocolException e) {
Log.e("Client Protocol Exception", "Error: " + e.getMessage());
}
catch (IOException e) {
Log.e("IO Exception", "Error: " + e.getMessage());
}
JSONObject obj = null;
JSONArray array = null;
try {
obj = new JSONObject(str);
array = obj.getJSONArray("Array");
}
catch (JSONException e) {
Log.e("JSON Exception", "Error: " + e.getMessage());
}
return array;
}
How to solve it or how to read the JSON text ..
Thanks ,,