0

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 ,,

1
  • share how you instantiated array Commented Feb 24, 2016 at 18:50

2 Answers 2

1

At first you need to create a class like this one:

public class ExampleClass { 
    int id; 
    String name; 
}

And then you call it in your try:

Gson gson = new Gson(); 
Type listOfTestObject = new TypeToken<ArrayList<ExampleClass>>() { }.getType(); 
ArrayList<ExampleClass> arrayInJSON = gson.fromJson(exampleJSONObject, listOfTestObject);

Then, you can use the response from JSON to get the size:

arrayInJSON.size();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but it hard .. from my code I get the json array, but how to get the size without this method, any another method !!!
But is only copy first block of code and put outside of onCreate method and take the second block and put it in your try, below "jsonObject = array.getJSONObject(i);". Use the "jsonObject" here: ArrayList<ExampleClass> arrayInJSON = gson.fromJson(jsonObject, listOfTestObject);
0

It is an array with one element on it [{ "1":{"id":"0","name":"first"}, "2":{"id":"1","name":"second"} }]

  becouse it look like [{...}] 

  here {...} is a map of 2 elements it is not array 
  jsonObject.getString("1").getString("id") in your code should return     what you need 

4 Comments

Thanks :) How I can know count the elements ??
I can not use .getString("1").getString("id") So I create second json object jsonObject = array.getJSONObject(i); json = new JSONObject(jsonObject.getString("1")); json.getString("id");
yes i can not sorry. optJSONObject("1").getString("id")

Your Answer

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.