java.lang.IllegalStateException: 这不是一个 JSON 数组
java.lang.IllegalStateException: This is not a JSON Array
我有两个问题:
1) java.lang.IllegalStateException: 这不是一个 JSON 数组
这是我在尝试将 List 转换为 JSON 时得到的。另外请告诉我如何将空数组处理为 json。
2) 当我 运行 查询数据库时,它为我获取了 5 个结果。当我调试 OperatorDetailsDao.java 时,我只得到 rs.next() 到 运行 一次。另一个问题是,尽管它是运算符数组,但我在 operatorList 中得到的结果为空。
麻烦帮我解决一下,卡了两天。
控制器Class
public class SearchController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
try {
String term = request.getParameter("terms");
System.out.println("Data from ajax call " + term);
OperatorDetialsDao details = new OperatorDetialsDao();
List<OperatorDetailsBean> operatorList = details.getOperators(term);
System.out.println("Operator List " + operatorList);
JsonElement element = new Gson().toJsonTree(operatorList);
System.out.println(element);
JsonArray jsonArray = element.getAsJsonArray();
response.getWriter().print(jsonArray);
response.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
OperatorDetailsDao -> getOperators()
public List<OperatorDetailsBean> getOperators(String name) {
PreparedStatement ps = null;
List<OperatorDetailsBean> operatorList = new ArrayList();
operatorList = null;
name = name.toUpperCase();
String data;
try {
ps = connection.prepareStatement("SELECT * FROM operator_details_m WHERE upper(name) LIKE ?");
ps.setString(1, name + '%');
ResultSet rs = ps.executeQuery();
while (rs.next()) {
uname = rs.getString("NAME");
token = rs.getString("TOKEN_NO");
OperatorDetailsBean op = new OperatorDetailsBean();
op.setName(uname);
op.setTokenNo(token);
operatorList.add(op);
}
} catch (Exception e) {
//System.out.println(e.getMessage());
}
return operatorList;
}
OperatorDetailsBean 是一个具有 getter 和设置器的 bean class。
这两个问题似乎都与 OperatorDetailsDao.getOperators()
方法中 operatorList
的错误初始化有关。下面是问题的注释解释:
public List<OperatorDetailsBean> getOperators(String name) {
PreparedStatement ps = null;
List<OperatorDetailsBean> operatorList = new ArrayList();
//Prahaladd - Setting this to null will cause operatorList to be null.
// This is not being re-initalized anywhere else
operatorList = null;
name = name.toUpperCase();
String data;
try {
ps = connection.prepareStatement("SELECT * FROM operator_details_m WHERE upper(name) LIKE ?");
ps.setString(1, name + '%');
ResultSet rs = ps.executeQuery();
while (rs.next()) {
uname = rs.getString("NAME");
token = rs.getString("TOKEN_NO");
OperatorDetailsBean op = new OperatorDetailsBean();
op.setName(uname);
op.setTokenNo(token);
operatorList.add(op); //Prahaladd - definitely a NullPointerException
}
} catch (Exception e) {
//Prahaladd - Oops!! NPE thrown above was gulped.
//System.out.println(e.getMessage());
}
return operatorList; //returned operatorList is null. Will impact gson library processing
}
现在在控制器中调用此方法时:
//Prahaladd - operatorList returned would be null
List<OperatorDetailsBean> operatorList = details.getOperators(term);
System.out.println("Operator List " + operatorList);
//Prahaladd - GSON processes this null value.
JsonElement element = new Gson().toJsonTree(operatorList);
System.out.println(element);
//Prahaladd - this will fail since we are trying to process a non-array
// as a JSON array.
JsonArray jsonArray = element.getAsJsonArray();
删除有问题的行 operatorList = null
将确保有效的 empty 数组列表和 not a null 值将被返回并被 GSON 正确处理。
附带说明 - GSON 库中有一个 issue logged 无法处理 NULL 数组,根据评论,这已得到修复。
我有两个问题:
1) java.lang.IllegalStateException: 这不是一个 JSON 数组
这是我在尝试将 List 转换为 JSON 时得到的。另外请告诉我如何将空数组处理为 json。
2) 当我 运行 查询数据库时,它为我获取了 5 个结果。当我调试 OperatorDetailsDao.java 时,我只得到 rs.next() 到 运行 一次。另一个问题是,尽管它是运算符数组,但我在 operatorList 中得到的结果为空。
麻烦帮我解决一下,卡了两天。
控制器Class
public class SearchController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
try {
String term = request.getParameter("terms");
System.out.println("Data from ajax call " + term);
OperatorDetialsDao details = new OperatorDetialsDao();
List<OperatorDetailsBean> operatorList = details.getOperators(term);
System.out.println("Operator List " + operatorList);
JsonElement element = new Gson().toJsonTree(operatorList);
System.out.println(element);
JsonArray jsonArray = element.getAsJsonArray();
response.getWriter().print(jsonArray);
response.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
OperatorDetailsDao -> getOperators()
public List<OperatorDetailsBean> getOperators(String name) {
PreparedStatement ps = null;
List<OperatorDetailsBean> operatorList = new ArrayList();
operatorList = null;
name = name.toUpperCase();
String data;
try {
ps = connection.prepareStatement("SELECT * FROM operator_details_m WHERE upper(name) LIKE ?");
ps.setString(1, name + '%');
ResultSet rs = ps.executeQuery();
while (rs.next()) {
uname = rs.getString("NAME");
token = rs.getString("TOKEN_NO");
OperatorDetailsBean op = new OperatorDetailsBean();
op.setName(uname);
op.setTokenNo(token);
operatorList.add(op);
}
} catch (Exception e) {
//System.out.println(e.getMessage());
}
return operatorList;
}
OperatorDetailsBean 是一个具有 getter 和设置器的 bean class。
这两个问题似乎都与 OperatorDetailsDao.getOperators()
方法中 operatorList
的错误初始化有关。下面是问题的注释解释:
public List<OperatorDetailsBean> getOperators(String name) {
PreparedStatement ps = null;
List<OperatorDetailsBean> operatorList = new ArrayList();
//Prahaladd - Setting this to null will cause operatorList to be null.
// This is not being re-initalized anywhere else
operatorList = null;
name = name.toUpperCase();
String data;
try {
ps = connection.prepareStatement("SELECT * FROM operator_details_m WHERE upper(name) LIKE ?");
ps.setString(1, name + '%');
ResultSet rs = ps.executeQuery();
while (rs.next()) {
uname = rs.getString("NAME");
token = rs.getString("TOKEN_NO");
OperatorDetailsBean op = new OperatorDetailsBean();
op.setName(uname);
op.setTokenNo(token);
operatorList.add(op); //Prahaladd - definitely a NullPointerException
}
} catch (Exception e) {
//Prahaladd - Oops!! NPE thrown above was gulped.
//System.out.println(e.getMessage());
}
return operatorList; //returned operatorList is null. Will impact gson library processing
}
现在在控制器中调用此方法时:
//Prahaladd - operatorList returned would be null
List<OperatorDetailsBean> operatorList = details.getOperators(term);
System.out.println("Operator List " + operatorList);
//Prahaladd - GSON processes this null value.
JsonElement element = new Gson().toJsonTree(operatorList);
System.out.println(element);
//Prahaladd - this will fail since we are trying to process a non-array
// as a JSON array.
JsonArray jsonArray = element.getAsJsonArray();
删除有问题的行 operatorList = null
将确保有效的 empty 数组列表和 not a null 值将被返回并被 GSON 正确处理。
附带说明 - GSON 库中有一个 issue logged 无法处理 NULL 数组,根据评论,这已得到修复。