1

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;
    }
}
3
  • 1
    Can you show the class MoodleDiscussion Commented Sep 7, 2016 at 4:25
  • Use this tool : jsonschema2pojo.org to parse json Commented Sep 7, 2016 at 4:26
  • @AkshayDusane don't post code in the comment.Editing the question will be better option. Commented Sep 7, 2016 at 5:22

3 Answers 3

1

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(); 
Sign up to request clarification or add additional context in comments.

Comments

0

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());

Comments

0

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);

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.