返回数据解析错误

com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path

期望返回一个对象但是却返回了一个数组

解决办法:

1.在参数中修改期望返回类 Student 为 LIst< Student>,这样才能解析到数据。

2.用 TypeToken 转一下:

Gson gson = new Gson();
String result = response;

ArrayList<Student> list = new ArrayList<Student>();
Type listType = new TypeToken<List<Student>>() {}.getType();
list = gson.fromJson(result, listType);

参考自:
http://stackoverflow.com/questions/18709730/com-google-gson-jsonsyntaxexception-java-lang-illegalstateexception-expected-b?rq=1
http://stackoverflow.com/questions/21520390/javax-ejb-ejbexception-com-google-gson-jsonsyntaxexception-java-lang-illegalst?rq=1

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-06-14
  • 2021-04-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-07
  • 2021-08-01
  • 2021-10-08
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
相关资源
相似解决方案