
我想对阵列使用GSON.
我看了一些示例,但无法使其与我的代码一起使用.
Using GSON to parse a JSON array.
我收到此错误消息:预期为字符串,但在行1列为BEGIN_ARRAY
在该项目中,我遵循的原始教程涉及解析Json Objects.
我的杰森:
[{ "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"}]我的代码:
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; 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; }}提前致谢
解决方法:
你的课应该是
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; }}GSON代码的代码应为:
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全部内容,希望文章能够帮你解决数组-GSON数组,错误消息:预期为字符串,但为BEGIN_ARRAY所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)