2

I'm trying to get elements from my json array.

this is my json response:

{"IDs":["635426812801493839","635429094450867472","635433640807558204"]}

This is what I've tried so far:

itemList = new ArrayList<HashMap<String, String>>();

            JSONArray a = jsonObj.getJSONArray(Constants.IDS);
            int arrSize = a.length();
            ArrayList<String> stringArray = new ArrayList<String>();
            for (int i = 0; i < arrSize; ++i) {
                JSONObject obj = a.getJSONObject(i);
                stringArray.add(obj.toString());
                item = new HashMap<String, String>();
                item.put(Constants.ID, obj.toString());
                itemList.add(item);
            }

            Log.e("ARR COUNT", "" + stringArray.size());

But I'm getting empty list. What is wrong with my code? Any help will be truly appreciated. Thanks.

0

3 Answers 3

5

The for loop should be

for (int i = 0; i < arrSize; ++i) {
      stringArray.add(a.getString(i));

your the JSONArray contains already string

Sign up to request clarification or add additional context in comments.

Comments

2
JSONObject obj = a.getJSONObject(i);

replace with

String str = a.getString(i);

1 Comment

thanks man for this however I chose @Blackbelt's answer as it contains explanation.
0

Use this

itemList = new ArrayList<HashMap<String, String>>();

JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {                
    stringArray.add(a.getString(i));
    item = new HashMap<String, String>();
    item.put(Constants.ID, obj.toString());
    itemList.add(item);
}

Log.e("ARR COUNT", "" + stringArray.size());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.