我正在使用Jackson JSON API在Java中創建JSON對象,然後進行打印。Jackson將來自OracleResultSet的數值附在引號周圍
我的代碼是類似於:
OracleResultSet rs = getMyResultSet();
OracleResultSetMetaData rsmd = (OracleResultSetMetaData)rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] columnNames = new String[columnCount];
int[] columntypes = new int[columnCount];
for(int i = 1; i < columnCount; ++i) {
columnNames[i] = rsmd.getColumnName(i);
columnTypes[i] = rsmd.getColumnTypes(i);
}
ObjectMapper mapper = new ObjectMapper();
ObjectNode node;
String columnName;
while(rs.next()) {
node = mapper.createObjectNode();
for(int i = 1; i < columnCount; ++i) {
columnName = columnNames[i];
//I have a case defined for every OracleTypes.XXXX, but for brevity I'm only putting the one I'm having issues with here
switch (columnTypes[i]) {
case: OracleTypes.NUMBER:
node.put(columnName, rs.getBigDecimal(i)); //BigDecimal chosen based on: http://docs.oracle.com/cd/B19306_01/java.102/b14188/datamap.htm
break;
default:
node.put(columnName, rs.getString(i));
}
//UPDATE: the next line is the logic error that I had that was casuing the trouble
node.put(columnName, rs.getString(i));
}
System.out.println(mapper.writeValueAsString(node));
}
這一切的偉大工程。問題是當我去打印它。
從OracleTypes.NUMBER
案件中獲得的任何值都會被引用,即使它們不應該是。樣本字符串,我得到:
{"month":"1","year":"2013","modified":"2013-02-05 13:41:48.0","net":"294.68"}
我想要什麼:
{"month":"1","year":"2013","modified":"2013-02-05 13:41:48.0","net":294.68}
爲什麼被打印帶引號的BigDecimal(或從我的測試任何非INT)?我的理解是,JSON中的所有真實數值都應該打印出不帶引號。我認爲這可能是一個原始/對象問題,並試圖從BigDecimal獲得雙重價值,但這並沒有幫助。
我已經使用調試器測試了該程序,它確實使用了switch-case語句,所以這不是問題。
你打開了[寫數字爲字符串](http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.jackson/jackson-core-asl/1.9.5/org /codehaus/jackson/JsonGenerator.java#JsonGenerator.Feature.0WRITE_NUMBERS_AS_STRINGS)功能錯誤? – 2015-02-10 19:30:32