3

I want to use GSON for my Array.

I looked at a few examples but could not get it to work with my code.
Using GSON to parse a JSON array.

I get this error message: Expected a string but was BEGIN_ARRAY at line 1 column

The orginal tutorial, I followed for this project covered parsing Json Objects.

My Json:

[{
   "nid": "25",
   "title": "angry guy",
   "body": "fhjk gjj"
}, {
   "nid": "24",
   "title": "25 mobile",
   "body": "25 test tes"
}, {
   "nid": "8",
   "title": "new post 4",
   "body": "sdfsdf sdfsdf"
}, {
   "nid": "7",
   "title": "new post",
   "body": "sdf sdf sdfsdf"
}]

My Code:

String finalJson = buffer.toString();
            JSONArray parentArray = new JSONArray(finalJson);
            List<ExerciseModel> exerciseModelList = new ArrayList<>();

            Gson gson = new Gson();
            for(int i=0; i<parentArray.length(); i++){
                JSONObject finalObject = parentArray.getJSONObject(i);
                ExerciseModel exerciseModel = gson.fromJson(finalObject.toString(), ExerciseModel.class);
                exerciseModelList.add(exerciseModel);
            }

            return exerciseModelList;

My Model:

    public class ExerciseModel {

    private int nid;
    private String title;
    private String body;

    public int getNid() {
        return nid;
    }
    public void setNid(int nid) {
        this.nid = nid;
    }
    public String getTitle() {
        return title;
    }
    public String toString() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
}

Thanks in advance

1
  • What is the problem? How didn't you get it working? What does it do wrong? Commented Aug 26, 2016 at 8:06

2 Answers 2

2

Your class should be

public class ExerciseModel
{
  private String nid;

  public String getNid() { return this.nid; }

  public void setNid(String nid) { this.nid = nid; }

  private String title;

  public String getTitle() { return this.title; }

  public void setTitle(String title) { this.title = title; }

  private String body;

  public String getBody() { return this.body; }

  public void setBody(String body) { this.body = body; }

}

And code for GSON code should be:

String json = "[{ \"nid\": \"25\", \"title\": \"angry guy\", \"body\": \"fhjk gjj\" }, { \"nid\": \"24\", \"title\": \"25 mobile\", \"body\": \"25 test tes\" }, { \"nid\": \"8\", \"title\": \"new post 4\", \"body\": \"sdfsdf sdfsdf\" }, { \"nid\": \"7\", \"title\": \"new post\", \"body\": \"sdf sdf sdfsdf\" }]";
Type listOfTestObject = new TypeToken<List<ExerciseModel>>() {}.getType();
ArrayList<ExerciseModel> models = new Gson().fromJson(json, listOfTestObject);
System.out.println(models.get(0).getTitle());
Sign up to request clarification or add additional context in comments.

3 Comments

Hallo Vuk, Where did you place the GSON code? Did you put it inside this for loop: Gson gson = new Gson(); for(int i=0; i<parentArray.length(); i++){ //code here }
Hi, No this code gets whole list of objects for JSON. You are getting ArrayList of object from this code so there is no need for using loop. You just change json variable in my case with your finalJson variable.
What exactly is different here?
0

To get an array of your model, simply do this

ExerciseModel[] models=gson.toJson(stringToBeParsed, ExerciseModel[].class);

To convert it to a java.util.List:

Type modelListType = new TypeToken<List<ExerciseModel>>(){}.getType();
List<ExerciseModel> modelList = gson.fromJson(stringToBeParsed, modelListType);

where, as the name suggests, stringToBeParsed is a String containing the json array. Check out the examples given in the user guide

Also, since nid is a String in your json, it should be of type String in ExerciseModel class as well

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.