I created a web service that returns a JSON formatted string in output but I have an error with the JSON parsing:
Unexpected token B in JSON at position 46
I tried to debug the program but I didn't find the error.
Here's the method that returns the 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\" } ]");
}
}
The JSON output looks like this:
[
{
"account_no": 77,
"data": "",
"quote_no": [B@7a9e5ed5,
"codpag": " 56",
"pag": "( 56) BONIFICO BANCARIO 120 GG DF",
"codage": " 150",
"agente": "( 150) 150 STRUTTURA PROVA"
}
]
But it should return this:
[
{
"account_no": 77,
"data": "",
"quote_no": "PREV1400001",
"codpag": " 56",
"pag": "( 56) BONIFICO BANCARIO 120 GG DF",
"codage": " 150",
"agente": "( 150) 150 STRUTTURA PROVA"
}
]