Akshay Dusane
Akshay Dusane

Reputation: 310

Parsing Json Object to Json Array using Gson

I am calling a webservice which gives me a json like this

{
    "discussions": [{
        "id": 54,
        "name": "Test Discusssion",
        "discussion": 41,
        "created": 1472816138,
        "modified": 1472816138,
        "subject": "Test Discusssion",
        "message": "<p>Welcome all to test discussion<\/p>",
    }],
    "warnings": []
}

But in android I am parsing it as

ArrayList<MoodleDiscussion> mDiscussions = gson.fromJson(reader,
      new TypeToken<List<MoodleDiscussion>>() {
      }.getType());

And the error I am getting is

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT

I want to convert the received json into an array how should I ?

Here is the MoodleDiscussion class

public class MoodleDiscussion extends SugarRecord < MoodleDiscussion > {@
    SerializedName("id") int discussionid;@
    SerializedName("name") String name;@
    SerializedName("subject") String subject;
    public int getDiscussionid() {
        return discussionid;
    }
    public String getName() {
        return name;
    }
    public int getUserid() {
        return userid;
    }
    public String getSubject() {
        return subject;
    }
}

Upvotes: 1

Views: 2411

Answers (3)

Rahul Chaudhary
Rahul Chaudhary

Reputation: 1061

you can parse in two ways first get array list of json object and loop it

   JSONArray discussionsList = obj1.getJSONArray("discussions");
            for (int i = 0; i < discussionsList.length(); i++) {
                JSONObject jsonObject = orderList.getJSONObject(i);
                Type type = new TypeToken<MoodleDiscussion>() {
                }.getType();
                CompletedOrderData completedOrderData = new Gson().fromJson(jsonObject.toString(), type);
                disscussionDataArrayList.add(completedOrderData);

and second way is parse jsonArray object as below

 Type type = new TypeToken<ArrayList<MoodleDiscussion>>() {
            }.getType();
            disscussionDataArrayList=new Gson().fromJson(orderList,type);

Upvotes: 0

ThanhTranIT
ThanhTranIT

Reputation: 31

Define class the same:

class Discussion implements Serializable
{
    @SerializedName("id")
    int id;
    @SerializedName("name")
    String name;
    @SerializedName("discussion")
    String discussion;
    bla bla
}

and:

class ResultResponse implements Serializable 
{
    @SerializedName("discussions")
    List<Discussion> mDiscussion = new ArrayList<>();
}

I think that is enough to parser using gson. You can not using below code in this case :)

ArrayList<MoodleDiscussion> mDiscussions = gson.fromJson(reader,
      new TypeToken<List<MoodleDiscussion>>() {
      }.getType());

Upvotes: 0

Asaph
Asaph

Reputation: 162859

The list you're trying to parse is contained within a nested json data structure that also needs to be represented in java classes if Gson is to parse it correctly.

You'll need a container class that looks something like this:

public class MoodleDiscussionResponse {
    private List<MoodleDiscussion> discussions;
    private List<Object> warnings;

    public List<MoodleDiscussion> getDiscussions() {
        return discussions;
    }

    public List<Object> getWarnings() {
        return warnings;
    }
}

Then you should be able to read it like so:

MoodleDiscussionResponse response = gson.fromJson(reader, MoodleDiscussionResponse.class);
List<MoodleDiscussion> mDiscussions = response.getDiscussions(); 

Upvotes: 1

Related Questions