Salve a tutti voglio convertire una query in risultato json ma mi da un errore strano
codice metodo:
Mi stampa al posto del json questo errore:
There was an error parsing JSON data
Unexpected token } in JSON at position 2
codice metodo:
Codice:
public String executeQueryTOJSON(String sql, String methodName, String className) // metodo utilizzato per eseguire i servizi di GET
{
String error = "";
System.out.println("E' stato utilizzato il metodo : " + methodName + " della classe :" + className);
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
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{
count++;
json.append("{ ");
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().getColumnName(i) + "\":");
switch (rsmd.getColumnType(i)) // switch per il controllo del valore da andar a prendere
{
case java.sql.Types.VARCHAR:
{
json.append("\"" + rs.getString(i) + "\"");
}
break;
case java.sql.Types.NULL:
{
json.append("null");
}
break;
case java.sql.Types.DATE:
{
json.append("\"" + rs.getDate(i) + "\"");
check = true;
}
break;
case java.sql.Types.INTEGER:
{ //4
json.append("\"" + rs.getInt(i) + "\"");
check = true;
}
break;
default:
{
if (check == false)
json.append(rs.getObject(i).toString());
}
break;
}
json.append(" , ");
} //End For
json.setCharAt(json.length() - 2, '}');
json.append(" , ");
if (count == 0) {
json.append("\"risultato\":\"errore valore nullo\" } ");
}
}//fine while
json.setCharAt(json.length() - 2, ']');
rs.close();
stmt.close();
} catch (SQLException e) {
return error = ("{ \"risultato\":\"errore query\" } ]");
}
return json.toString(); // output della Stringa JSON
} else {
return error = ("{ \"risultato\":\"errore connessione\" } ]");
}
}
Mi stampa al posto del json questo errore:
There was an error parsing JSON data
Unexpected token } in JSON at position 2