返回數據解析錯誤
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