0

So I am trying to get values from a JSONArray into a JSONObject but I am constantly getting this error: org.json.JSONException: Value Aamod Shoghi at 0 of type java.lang.String cannot be converted to JSONObject

JSONObject json = jParser.makeHttpRequest(url_all_resorts, "POST", params);
            Log.d("All Resorts : ", json.toString());
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // resorts found
                // Getting Array of resorts
                resorts = json.getJSONArray(TAG_RESORT);
                rates = json.getJSONArray(TAG_PRICE);

                int i;

                // looping through All resorts
                for (i = 0; i < resorts.length() & i < rates.length(); i++) {
                    JSONObject c = resorts.getJSONObject(i);
                    JSONObject d = rates.getJSONObject(i);

                    // Storing each json item in variable
                    String name = c.getString(TAG_NAME);
                    String price = d.getString(TAG_ROOM_PRICE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_NAME, name);
                    map.put(TAG_ROOM_PRICE, price);

                    // adding HashList to ArrayList
                    resortsList.add(map);
                }

            }

JSON sample data

{"resorts":["Aamod Shoghi","Aamod Shoghi","Aamod Shoghi"],"room_prices":["100","800","1000"],"success":1}
2
  • 1
    Post your json sample data Commented May 28, 2017 at 20:01
  • Added the data @FAT Commented May 28, 2017 at 20:09

1 Answer 1

1

Your resorts and room_prices array contains strings only.

Use:

// Storing each json item in variable
String name = resorts.getString(i);
String price = rates.getString(i);

Instead of:

JSONObject c = resorts.getJSONObject(i);
JSONObject d = rates.getJSONObject(i);

// Storing each json item in variable
String name = c.getString(TAG_NAME);
String price = d.getString(TAG_ROOM_PRICE);

Try this:

        ...........
        ...................

        if (success == 1) {
            // resorts found
            // Getting Array of resorts
            resorts = json.getJSONArray("resorts");
            rates = json.getJSONArray("room_prices");

            int i;

            // looping through All resorts
            for (i = 0; i < resorts.length() & i < rates.length(); i++) {
                // Storing each json item in variable
                String name = resorts.getString(i);
                String price = rates.getString(i);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_NAME, name);
                map.put(TAG_ROOM_PRICE, price);

                // adding HashList to ArrayList
                resortsList.add(map);
            }

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

Comments

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.