-1

I understand that what I'm fetching is already an array but still I'm not sure what to change here.

I have this which is returning data fine but I'm not sure what to do with the array to get the values into the map.

protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.mywebsite.club/api/coffees");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("coffees");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("title", jsonobject.getString("title"));
                map.put("brand", jsonobject.getString("brand"));
                map.put("price", jsonobject.getInt("price"));
                map.put("brandlogo", jsonobject.getString("brandlogo"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
Oceans
  • 3,445
  • 2
  • 17
  • 38
ChrisM
  • 706
  • 11
  • 30
  • Paste the server response you want to parse – Farhad Feb 25 '16 at 15:20
  • You can just append json array in response from the url. "{coffees:" + url_reponse + "}" after that u can go ahead and get jsonarray as coffees – Vijay Feb 25 '16 at 15:30
  • From your response, I didn't find **coffees** attribute as array name, in your json response you have only array without attribute name. So you don't need to get json object from response, whatever your response string just get json array from that like, `JSONArray jsonarray = new JSONArray(jsonobject.toString());` – user370305 Feb 25 '16 at 15:30
  • user370305 please post it as an aswer because it's right :) and comments are not for answering – bartol Feb 25 '16 at 15:30
  • @bartol - I knew, let just OP give chance to try this, if he will get success will post as answer. Thanks :) – user370305 Feb 25 '16 at 15:32
  • Possible duplicate of [Get JSONArray without array name?](http://stackoverflow.com/questions/10164741/get-jsonarray-without-array-name) – Yazan Feb 25 '16 at 15:33
  • I think, in your case, it should be `JSONArray jsonarray = new JSONArray(JSONfunctions .getJSONfromURL("http://www.mywebsite.club/api/coffees"));` – user370305 Feb 25 '16 at 15:38
  • Do i replace jsonarray = jsonobject.getJSONArray("coffees"); with that? Im not sure whaere to add that. maybe make an answer with modified version of my code. Thanks – ChrisM Feb 25 '16 at 15:40

1 Answers1

1

From your response, I didn't find coffees attribute as array name, in your json response you have only array without attribute name. So you don't need to get json object from response, whatever your response string just get json array from that like,

protected Void doInBackground(Void... params) {
    // Create an array
    arraylist = new ArrayList<HashMap<String, String>>();
    // Retrieve JSON Array from the given URL address
    jsonarray = new JSONArray(JSONfunctions.getJSONfromURL("http://www.mywebsite.club/api/coffees"));

    try {
         if(jsonarray != null)
         {

          for (int i = 0; i < jsonarray.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            jsonobject = jsonarray.getJSONObject(i);
            // Retrive JSON Objects
            map.put("title", jsonobject.getString("title"));
            map.put("brand", jsonobject.getString("brand"));
            map.put("price", jsonobject.getInt("price"));
            map.put("brandlogo", jsonobject.getString("brandlogo"));
            // Set the JSON Objects into the array
            arraylist.add(map);
          }
       }
    } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}
user370305
  • 108,599
  • 23
  • 164
  • 151