我是java和javascript的新手。我已经开始使用json对象从我们的SQL服务器返回数据,但它很好,但我遇到了一个问题,我不知道如何处理它。我甚至不确定它是否可能但是从来没有伤害过要求。
我在数据库中有一个存储HTML内容的字段。
<p>Please verify you have done the following before submitting a trouble-ticket</p> <ol> <li>Be sure you are using your correct user name and password (e.e., first initial and last name)</li> <li>Password are case sensitive</li> <li>Be sure to select the domain from the drop-down list.</li> <li>Network cable must be plugged in!</li> </ol>
我正在尝试将其输出到JSON对象,以便我可以在屏幕上显示它的当前格式。
我在.jsp页面中构建我的json对象,如下所示:
if(rs != null && rs.next()){
out.print("{\"qteTips\" : [");
tipList = new StringBuffer();
int rowCount = 0;
String HelpfulTipID = null;
String Title = null;
String Tip = null;
String DateCreated = null;
String Active = null;
do{
rowCount++;
HelpfulTipID = rs.getString("HelpfulTipID");
Title = rs.getString("Title");
DateCreated = rs.getString("DateCreated");
Active = rs.getString("Active");
Tip = rs.getString("Tip");
if(rowCount > 1){
out.print(",");
}
out.print("{\"HelpfulTipID\":\""+HelpfulTipID+"\",");
out.print("\"Title\":\""+Title+"\",");
out.print("\"DateCreated\":\""+DateCreated+"\",");
out.print("\"Active\":\""+Active+"\",");
out.print("\"Tip\":\""+Tip+"\"}");
if(DEBUG){
System.out.println("created record #"+rowCount);
}
}
while(rs != null && rs.next());
out.print("]}");
try {rs.close();} catch (SQLException sqle){}
try{ps.close();} catch (SQLException sqle){}
}else{
out.print("{\"error\":\"No tips found.\"}");
}
但当我抓住物体时,它看起来像这样:
{"qteTips" : [{"HelpfulTipID":"47","Title":"Unable to login to your comptuer?","DateCreated":"06/01/15","Active":"1","Tip":"<p>Please verify you have done the following before submitting a trouble-ticket</p>
<ol>
<li>Be sure you are using your correct user name and password (e.e., first initial and last name)</li>
<li>Password are case sensitive</li>
<li>Be sure to select the domain from the drop-down list.</li>
<li>Network cable must be plugged in!</li>
</ol>
"}]}
尝试验证时,我在jsonlint.com上收到此错误。
Parse error on line 8:
... "Tip": "<p>Please verify yo
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
有没有人知道我想要做的更好的方法?
答案 0 :(得分:0)
问题是返回的JSON中的换行符,您将不得不将它们转义为\n
。我经常这样做:
line = line.replaceAll('\r', '').replaceAll('\n', '\\n')
基本上将\r
字符替换为\n
字符,\\n
替换为...string goes here\nNext line
,这将在您的JSON上呈现为Ti.API.debug('Installing sql database "' + dbFile + '" with name "' + dbName + '"');
var db = Ti.Database.install(dbFile, dbName);
[DEBUG] Installing sql database "/trackit.sqlite" with name "trackit"
答案 1 :(得分:0)
我已经包含了一个jsp页面。
<%@ page contentType="text/html"%>
<%@include file="GetAllHelpfulTips.jsp"%>
<html lang="en">
<head>
该页面执行一个sql语句,该语句在循环时构建数据。
ps = conn.prepareStatement(sqlTask.toString());
rs = ps.executeQuery();
if(rs != null && rs.next()){
tipList = new StringBuffer();
int rowCount = 0;
String HelpfulTipID = null;
String Title = null;
String Tip = null;
String DateCreated = null;
String Active = null;
do{
rowCount++;
HelpfulTipID = rs.getString("HelpfulTipID");
Title = rs.getString("Title");
DateCreated = rs.getString("DateCreated");
Active = rs.getString("Active");
Tip = rs.getString("Tip");
if(tipsPage == "1"){
System.out.println("from tip page");
tipList.append("<div id='tip' class='tip'>"+
"<div class='title row'>"+
"<div class='col-xs-6'>"+
"<p class='tipTitle'>"+Title+"</p>"+
"</div>"+
"<div class='col-xs-3'>"+
"<input type='text' id='tipID' name='tipVal' value='"+HelpfulTipID+"'>"+
"</div>"+
"</div>"+
"<div class='tipDetail row'>"+
"<p>"+Tip+"</p>"+
"</div>"+
"</div>");
}else{
tipList.append("<div id='tipMgmt' class='tipMgmt'>"+
"<div class='row'>");
if(Active.equals("1")){
tipList.append("<div class='col-xs-1'><center><i class='fa fa-check active'></i></center></div>");
}else{
tipList.append("<div class='col-xs-1'><center><i class='fa fa-times inactive'></i></center></div>");
}
tipList.append("<div class='col-xs-3'><center>"+DateCreated+"</center></div>"+
"<div class='col-xs-6'><p class=''>"+Title+"</p></div>"+
"<div class='col-xs-1'><input type='text' style='display:none' size='1px' id='HelpfulTipID' name='HelpfulTipID' value='"+HelpfulTipID+"'></div>"+
"</div>"+
"</div>");
}
;
if(DEBUG){
System.out.println("created record #"+rowCount);
System.out.println(tipList);
}
}
while(rs != null && rs.next());
然后我在当前页面上包含了新对象。
<div>
<%=tipList%>
</div>
由于存储的数据是HTML,因此它与ckeditor中输入的数据完全相同!