GSON阵列,错误消息:期望一个字符串,但是BEGIN_ARRAY [英] GSON Array, Error message: Expected a string but was BEGIN_ARRAY
本文介绍了GSON阵列,错误消息:期望一个字符串,但是BEGIN_ARRAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的数组使用 GSON
。
我看了几个例子,但无法得到它与我的代码一起工作。
使用GSON解析一个JSON数组。
我得到这个错误信息:期望一个字符串,但是在第1行的BEGIN_ARRAY
这个项目的原始教程涵盖了解析Json Objects。
$ b My Json:
[{
nid:25,
title:生气的家伙 ,
body:fhjk gjj
},{
nid:24,
title:25 mobile,
身体:25测试tes
},{
nid:8,
title:新帖子4,
body: sdfsdf sdfsdf
,{
nid:7,
title:new post,
body:sdf sdf sdfsdf
$] b $ b
我的代码:
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;
我的模特:
public class ExerciseModel {
private int nid;
私有字符串标题;
私人字符串正文;
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;
$ / code $ / pre
预先致谢
$ b {
private String nid;
public String getNid(){return this.nid; }
public void setNid(String nid){this.nid = nid; }
私有字符串标题;
public String getTitle(){return this.title; }
public void setTitle(String title){this.title = title; }
私人字符串正文;
public String getBody(){return this.body; }
public void setBody(String body){this.body = body; }
$ / code>
GSON代码的代码应该是:
字符串json =[{\nid\:\25 \,\title \ :\\ \\生气的家伙\\,\\ \\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 \}];
类型listOfTestObject = new TypeToken< List< ExerciseModel>>(){} .getType();
ArrayList< ExerciseModel> models = new Gson()。fromJson(json,listOfTestObject);
System.out.println(models.get(0).getTitle());
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
解决方案
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());
这篇关于GSON阵列,错误消息:期望一个字符串,但是BEGIN_ARRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文