user1611830
user1611830

Reputation: 4867

can't fetch json object with correct format

Suppose I am fetching from an api some json array as so

[{"id":1,"title":"title","description":"description","vote":null,"created_at":"2013-11-12T21:08:10.922Z","updated_at":"2013-11-12T21:08:10.922Z"}]

I want to retrieve this json as an Some object from URL_Some url

public class Some implements Serializable {

    private String id;
    private String title;
    private String description;
    private String vote;
    private String created_at;
    private String updated_at;
    }//with all getters and setters

.

public List<Some> getSome() throws IOException {
        try {
            HttpRequest request = execute(HttpRequest.get(URL_Some));
            SomeWrapper response = fromJson(request, SomeWrapper.class);
            Field[] fields = response.getClass().getDeclaredFields();
            for (int i=0; i<fields.length; i++)
            {
                try {
                    Log.i("TAG", (String) fields[i].get(response));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                Log.i("TAG", fields[i].getName());
            }
            if (response != null && response.results != null)
                return response.results;
            return Collections.emptyList();
        } catch (HttpRequestException e) {
            throw e.getCause();
        }
    }

and SomeWrapper is simply

private static class SomeWrapper {

        private List<Some> results;
    }

The problem is that I keep on getting this message

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

PS : I use

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

import com.google.gson.JsonParseException;

Upvotes: 0

Views: 101

Answers (2)

gonzaw
gonzaw

Reputation: 781

Your json should actually be like this:

{"results": [{"id":1,"title":"title","description":"description","vote":null,"created_at":"2013-11-12T21:08:10.922Z","updated_at":"2013-11-12T21:08:10.922Z"}]}

Gson will try to parse the json and create a SomeWrapper object. This alone tells Gson he will wait for a json with this format {...} since he's expecting an object. However you passed an array instead, that's why it complains about expecting BEGIN_OBJECT ({) but getting BEGIN_ARRAY instead ([). After that, it will expect this json object to have a results field, which will hold an array of objects.

You can create List<Some> directly without the need of a wrapper class however. To do so do this instead:

Type type= new TypeToken<List<Some>>() {}.getType();
List<Some> someList = new GsonBuilder().create().fromJson(jsonArray, type);

In this case, you can use the original json array you posted.

Upvotes: 1

Myth1c
Myth1c

Reputation: 649

The JSON you posted is a JSON array, which is indicated by the squared brackets around it: [ ].

You'd have to read the first object from the JSON array.

I personally use the org.json packages for Android JSON and parse my JSON in a manner like this:

private void parseJSON(String jsonString) {
JSONArray json;
    try {
        json = new JSONArray(jsonString);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        String id = jsonObject.getString("id");
    } catch (JSONException jsonex) {
        jsonex.printStackTrace();
    }
}

If you've got multiple JSON objects in your array you can iterate over them by using a simple for loop (not for each!)

Upvotes: 0

Related Questions