为什么我的 json 字符串中有 28 个不可打印的字符?
我正在 JSP 中的数据库查询生成一个 JSON 字符串,但它总是包含不可打印的字符,我不明白为什么!
生成 JSON 的 JSP 如下:
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();
这是我在 PHP 中用于显示的代码:
echo '*'.$json.'*<br>';
echo strlen($json).'<br>';
$json = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json);
echo '*'.$json.'*<br>';
echo strlen($json).'<br>';
其中显示:
* [{"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
因此有 28 个不可打印字符的差异 - 大部分在开头。它们如何到达那里以及如何在 JSP 中摆脱它们?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 JSP 是 HTTP 响应的视图技术的一部分。
<% %>
之外的所有内容也会发送到响应,包括空格和换行符(右键单击 PHP 生成的 HTML 页面,执行查看源代码,您将您自己也可以看到这些换行符,这些换行符您将被视为“不可打印”字符)。删除
<% %>
之外的所有空格和换行符,或者更好的是,使用 servlet反而。您可以在如何使用 Servlet 的答案中找到一些生成 JSON 的 Servlet 的启动示例和阿贾克斯?Because JSP is as being a view technology part of the HTTP response. Everything outside
<% %>
is sent to the response as well, including whitespace and newlines (rightclick the HTML page produced by PHP, do View Source and you'll see those newlines yourself as well, it are those newlines which you account to "unprintable" characters).Remove any whitespace and newlines outside
<% %>
or, better, use a servlet instead. You can find some kickoff examples of a JSON-producing Servlet in the answer to How to use Servlets and Ajax?