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

Community
  • 1
  • 1
Charm Geld
  • 191
  • 2
  • 17

2 Answers2

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());
Vuk Vasić
  • 1,398
  • 10
  • 27
  • 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 – Charm Geld Sep 20 '16 at 11:31
  • 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. – Vuk Vasić Sep 20 '16 at 11:33
  • What exactly is different here? – fobbymaster Feb 27 '17 at 21:12
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

Rohan Kamat
  • 239
  • 1
  • 6