问题
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:
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());
回答2:
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
来源:https://stackoverflow.com/questions/39161354/gson-array-error-message-expected-a-string-but-was-begin-array