Mohamed Ali Lassoued
Mohamed Ali Lassoued

Reputation: 311

Type JSONArray cannot be converted to JSONObject

When I call my service web i have this exception: org.json.JSONException:Value[{}] of type org.json.JSONArray cannot be converted to JSONObject

private void jsonParse(){
        String url="http://192.168.56.1:8095/rest/workouts/all";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("");

                            for(int i = 0; i < jsonArray.length(); i++) {
                                JSONObject w = jsonArray.getJSONObject(i);

                                String title = w.getString("title");
                                String goal = w.getString("goal");

                                txtv.append(title + ", " + goal +"\n\n");
                            }
                        ...

By the way this is the JSON Format

[{"id":"1","title":"title","goal":"goal","exercice":"Exercice 
1","difficulty":"Beginner","duration":"3","image":"..","description":"..."},]

Upvotes: 1

Views: 2779

Answers (6)

Mohamed Ali Lassoued
Mohamed Ali Lassoued

Reputation: 311

This is the solution that i found, thanks for all guys ♥ ..

    private void jsonParse(){
        String url="http://192.168.56.1:8095/rest/workouts/all";
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,url,null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray jsonArray) {
                        try {
                            
                            for(int i = 0; i < jsonArray.length(); i++) {


                                JSONObject jsonobject = jsonArray.getJSONObject(i);

                                String title = jsonobject.getString("title");
                                String goal = jsonobject.getString("goal");

                                txtv.append(title + ", " + goal +"\n\n");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        mQueue.add(jsonArrayRequest);
    }

Upvotes: 3

Alex
Alex

Reputation: 9342

Your json response is actually a JsonArray, so you need to change your code to:

private void jsonParse(){
    String url="http://192.168.56.1:8095/rest/workouts/all";
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray >() {
                @Override
                public void onResponse(JSONArray response) {
                    try {


                        for(int i = 0; i < response.length(); i++) {
                            JSONObject w = response.getJSONObject(i);

                            String title = w.getString("title");
                            String goal = w.getString("goal");

                            txtv.append(title + ", " + goal +"\n\n");
                        }

Upvotes: 1

Ramkumar.M
Ramkumar.M

Reputation: 691

try this ... It should be

JSONArray jsonarray = new JSONArray(response);

and then

for(int i=0; i < jsonarray.length(); i++) {
   JSONObject jsonobject = jsonarray.getJSONObject(i);

   //your code
}

Upvotes: 0

Rahul Chokshi
Rahul Chokshi

Reputation: 668

Try this one

try{

        JSONArray jsonArray = new JSONArray(response.string());
        for (int i=0;i<jsonArray.length();i++){

            String Title=jsonArray.getJSONObject(i).getString("title");
            String Goal=jsonArray.getJSONObject(i).getString("goal");

        }
    }
    catch (Exception e){
        e.printStackTrace();
    }

Upvotes: 1

Mohammed
Mohammed

Reputation: 136

Though the answers can handle your case, but looking at your JSON, it's clearly an array not an object, the correct format of your JSON could be something like this:

{"id":"1","title":"title","goal":"goal","exercice":"Exercice1","difficulty":"Beginner","duration":"3","image":"..","description":"..."}

Which have no "[", "]" and ",". So here's my solution, if it's expected to receive just a single Object all the time, you should change your API, sending you just an object as I mentioned above, otherwise, that "," at the end is incorrect for an array having just one single item, and anyhow, you have to change your model to except an array [which you can declare it as a list like: List<Model> myModelList] so that the parser will work fine.

Upvotes: 0

Benjamin
Benjamin

Reputation: 421

Try

 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                    Request.Method.GET,
                    url,
                    null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {

                            for(int i = 0; i < response.length(); i++) {
                            JSONObject w = response.getJSONObject(i);

                            String title = w.getString("title");
                            String goal = w.getString("goal");

                            txtv.append(title + ", " + goal +"\n\n");
                             }

                            }catch (JSONException e){
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener(){
                        @Override
                        public void onErrorResponse(VolleyError error){
                            // Do something when error occurred

                        }
                    }
            );

Upvotes: 0

Related Questions