Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON Array, Error message: Expected a string but was BEGIN_ARRAY

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

like image 248
Charm Geld Avatar asked Mar 25 '26 19:03

Charm Geld


1 Answers

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());
like image 61
Vuk Vasić Avatar answered Mar 27 '26 08:03

Vuk Vasić



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!