【发布时间】:2015-06-29 06:06:59
【问题描述】:
数据库表结构:
Card Number Name Bal Withdrawal Deposit Bal Tx Date Usage_Type
AL98****66325 MOSES 0 12981.02 1 -362764.96 16-Oct-14 N/A
AL98****66325 MOSES 0 50000.01 -362764.96 17-Oct-14 Manual
如您所见,"Deposit" 第二行的值为空。它不是零或空值。所以我得到异常说:
java.lang.NullPointerException
atcom.google.gson.JsonPrimitive.isPrimitiveOrString(JsonPrimitive.java:278)
at com.google.gson.JsonPrimitive.setValue(JsonPrimitive.java:100)
at com.google.gson.JsonPrimitive.<init>(JsonPrimitive.java:65)
at com.In10s.getTable.GetTable.doGet(GetTable.java:49)
我从我的客户那里获得了 xml 数据,并且可能有很多空值,我无法通过数百个文件来替换 emtpy 值。有可能解决这个问题吗?
有没有gson的替代品。我还没有尝试过杰克逊图书馆。我不知道如何将结果集转换为杰克逊中的 json 对象如果我必须转移。
Java 代码:
public class GetTable extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
JsonObject jsonResponse = new JsonObject();
JsonArray data = new JsonArray();
PrintWriter out = response.getWriter();
Connection con = OracleDBConnection.getConnection();
String query = "Select * from CREDIT_CARD_TRANSACTIONS";
Statement st = con.createStatement();
ResultSet rSet = st.executeQuery(query);
while (rSet.next())
{
JsonArray row = new JsonArray();
row.add(new JsonPrimitive(rSet.getString("CARD_NUMBER")));
row.add(new JsonPrimitive(rSet.getString("FIRST_NAME")));
row.add(new JsonPrimitive(rSet.getString("OPENING_BALANCE")));
row.add(new JsonPrimitive(rSet.getString("WITHDRAWAL")));
row.add(new JsonPrimitive(rSet.getString("DEPOSIT")));
row.add(new JsonPrimitive(rSet.getString("CLOSING_BAL")));
row.add(new JsonPrimitive(rSet.getString("TXDATE")));
row.add(new JsonPrimitive(rSet.getString("USAGE_TYPE")));
data.add(row);
}
jsonResponse.add("ResponseData", data);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(jsonResponse);
out.flush();
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】: