我创建了一个Web服务,该服务在输出中返回JSON格式的字符串,但是JSON解析出现错误:
Unexpected token B in JSON at position 46
我尝试调试程序,但没有找到错误。
这是返回JSON的方法:
public String executeQueryTOJSON(String sql) // metodo utilizzato per eseguire i servizi di GET
{
String error = "";
StringBuilder json = new StringBuilder("[ ");
if (_Connected) // controllo l'avvenuta connessione
{
try {
stmt = _conn.createStatement();
ResultSet rs = stmt.executeQuery(sql); // executeQuery è un comando che permette di eseguire le query di
// selezione e restituisce le righe del risultato della query
// System.out.println("query fatta");
// a= rs.getString("accountname");
java.sql.ResultSetMetaData rsmd = rs.getMetaData(); // oggetto rsmd con il comando getMetaData() viene
// utilizzato per scoprire le colonne dell'oggetto
// rs
int cols = rsmd.getColumnCount(); // il comando getColumnCount() serve per calcolare il numero di
// colonne dell'oggetto rsmd
int count = 0; // variabile di appoggio per controllare se si trasferisce un valore nullo
while (rs.next()) { // ciclo che si ripette in base alle righe di rs{
// String foundType = rs.getString(1);
// System.out.println(foundType);
count++;
json.append("{ ");
// errore precedente -> "< cols" non faceva il giusto ciclo di parsing
for (int i = 1; i <= cols; i++) // ciclo che si ripete per il numero oggetti situati nella tabella
{
boolean check = false;
json.append("\"" + rs.getMetaData().getColumnLabel(i) + "\":");
switch (rsmd.getColumnType(i)) // switch per il controllo del valore da andar a prendere
{
case java.sql.Types.VARCHAR: {
String tmp = rs.getString(i);
// System.out.println(tmp);
if (tmp == null)// confronto per vedere se il valore è uguale a null
{
json.append("null");
} else
// modifica effettuata con .replace per sostiruire i caratteri errati
json.append("\"" + rs.getString(i).replace("\"", "'") + "\"");// replace usata per fare
// il giusto parsing
}
break;
case java.sql.Types.CHAR: {
// System.out.println(json.toString());
String tmp = rs.getString(i);
if (tmp == null)// confronto per vedere se il valore è uguale a null
{
json.append("null");
} else
json.append("\"" + rs.getString(i).replace("\"", "'") + "\"");
}
break;
case java.sql.Types.NULL: {
json.append("null");
}
break;
case java.sql.Types.DATE: {
try {
rs.getDate(i);
// json.append("\"" + rs.getDate(i) + "\"");
// check = true;
} catch (SQLException e) {
} finally {
json.append("\"\"");
}
}
break;
case java.sql.Types.INTEGER: {
json.append(rs.getInt(i));
check = true;
}
break;
default: {
if (check == false)
json.append(rs.getObject(i).toString());
// System.out.println(json);
}
break;
}
json.append(" , ");
}
json.setCharAt(json.length() - 2, '}');
json.append(" , ");
if (count == 0) {
json.append("\"risultato\":\"errore valore nullo\" } ");
}
}
json.setCharAt(json.length() - 2, ']');
rs.close();
stmt.close();
_conn.close();// chiusura connessione con database
} catch (SQLException e) {
e.printStackTrace();
return error = ("{ \"risultato\":\"errore query\" } ]");
}
// System.out.println(json.toString());
return json.toString(); // output della Stringa JSON
} else {
return error = ("{ \"risultato\":\"errore connessione\" } ]");
}
}
JSON输出如下:
[
{
"account_no": 77,
"data": "",
"quote_no": [B@7a9e5ed5,
"codpag": " 56",
"pag": "( 56) BONIFICO BANCARIO 120 GG DF",
"codage": " 150",
"agente": "( 150) 150 STRUTTURA PROVA"
}
]
但是它应该返回这个:
[
{
"account_no": 77,
"data": "",
"quote_no": "PREV1400001",
"codpag": " 56",
"pag": "( 56) BONIFICO BANCARIO 120 GG DF",
"codage": " 150",
"agente": "( 150) 150 STRUTTURA PROVA"
}
]
答案 0 :(得分:0)
我在本节中认为:
default:
(check == false)
json.append(rs.getObject(i).toString());
//System.out.println(json);
}
您正在尝试将对象转换为字符串。除非您重写toString方法以打印值,否则它将始终打印[B @ 7a9e5ed5。此代码是对象的字符串值。 您不能直接将对象转换为字符串。
答案 1 :(得分:0)
您会看到B@7a9e5ed5
是您要显示的值的地址。
json.append(rs.getObject(i).toString()); In this line, the to string method must be overriden for your type of object.
例如,如果我有一个类为 Student 的对象student
,并且没有覆盖toString()
方法。如果我使用student.toString();
,它将在内存中打印学生对象的保存地址值。
例如,如果我想查看学生的值,则必须重写类中的toString方法。
@Override
public String toString(){
return this.getName() + " " + this.getClass();
}
以上只是一个示例,在您的代码中,您需要知道从结果集中获取的对象类型,并且需要重写该类中的toString方法。
希望这很有道理。
答案 2 :(得分:0)
检查标志仅对于integer为true。
对于默认块中的其他类型,您需要将对象直接转换为非字符值的字符串,您需要重写toString方法以进行准确的转换。请确保您重写toString或提供String.valueOf并提供适当的例外捕获机制