I am producing a JSON string from a database query in JSP but it always has unprintable characters in it and I don't understand why!
The JSP to produce the JSON is below:
String user = "username"; // set a username
String password = "password"; // set a password
Class.forName("org.firebirdsql.jdbc.FBDriver"); // JDBC for firebird a.k.a. Jaybird
String DB = "jdbc:firebirdsql://123.123.123.123:3050/C:\\db.fdb";
JSONArray obj=new JSONArray(); //Creating the json object
Connection connection = DriverManager.getConnection(DB, user, password);
Statement statement = connection.createStatement();
int i=0;
Class.forName("org.firebirdsql.jdbc.FBDriver"); // JDBC for firebird a.k.a. Jaybird
String query = "SELECT ses.sessionid, ses.datetime, ses.guid, ses.staffid FROM session ses ORDER by ses.datetime";
ResultSet resultset = statement.executeQuery(query);
while (resultset.next())
{
JSONObject j = new JSONObject();
j.put("SessionID", resultset.getString("sessionid"));
j.put("DateTime", resultset.getString("datetime"));
j.put("GUID", resultset.getString("guid"));
j.put("StaffID", resultset.getString("staffid"));
obj.add(i, j);
i++; // Counter for indexing the JSONArray
}
resultset.close();
statement.close();
connection.close();
And this is the code I am using in PHP to display:
echo '*'.$json.'*<br>';
echo strlen($json).'<br>';
$json = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json);
echo '*'.$json.'*<br>';
echo strlen($json).'<br>';
which shows:
* [{"SessionID":"850","DateTime":"2011-10-03 14:21:37.0","GUID":"51e71c19-ca13-4053-bd95-2addb5ba69f6","StaffID":"804"}] *
146
*[{"SessionID":"850","DateTime":"2011-10-03 14:21:37.0","GUID":"51e71c19-ca13-4053-bd95-2addb5ba69f6","StaffID":"804"}]*
118
So a difference of 28 unprintable characters - mostly at the beginning. How are they getting there and how can I get rid of them in the JSP?
Thanks