简体   繁体   English

新行,双引号引起json数据错误

[英]new line, double quote giving error in json data

I am creating JSON data in JSF Manually. 我正在JSF中手动创建JSON数据。

Below is what I am doing & is working perfectly. 以下是我正在做的事情,并且工作正常。

JSF Page JSF页面

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:body>
        <ui:composition
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:ui="http://java.sun.com/jsf/facelets">
            <f:event type="preRenderView" listener="#{MobileLogin.fetNewsData()}" />
        </ui:composition>
    </h:body>
</html>

MobileLogin.java MobileLogin.java

public void fetNewsData() {
    try {
        ConnectToDatabase db = new ConnectToDatabase();
        Connection conn = db.makeconnection();
        PreparedStatement psmt = conn.prepareStatement("SELECT * FROM news ORDER BY id DESC");
        ResultSet rs = psmt.executeQuery();

        String json = "[";
        int myTotal = 0;
        String myDate = "";
        while (rs.next()) {
            System.out.println("test 002 - " + rs.getString(1));
            if (myTotal >= 1) {
                json = json + ",";
            }
            myDate = rs.getString(4);
            myDate = myDate.substring(8, 10) + "-" + myDate.substring(5, 7) + "-" + myDate.substring(0, 4);
            json = json + "{"
                    + "\"id\": \"" + rs.getString(1) + "\","
                    + "\"title\": \"" + rs.getString(2) + "\","
                    + "\"detailMessage\": \"" + rs.getString(3) + "\","
                    + "\"whenAdd\": \"" + myDate + "\""
                    + "}";
            myTotal++;
        }
        json = json + "]";

        // Show it.
        myNewsInJSONFormat = json.toString();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setResponseContentType("application/json");
        externalContext.setResponseCharacterEncoding("UTF-8");
        externalContext.getResponseOutputWriter().write(myNewsInJSONFormat);
        facesContext.responseComplete();
        System.out.println("fina data is " + myNewsInJSONFormat);

        if (conn!=null) {
            conn.close();
        }

    } catch (Exception e) {
        myNewsInJSONFormat = "";
    }
}

All was working perfectly, but the problem occurs WHEN I have new line and double quotes in text (especially at detailMessage). 一切工作正常,但是当我在文本中有换行符和双引号时(特别是在detailMessage中),就会出现问题。

Any workaround for new line and double quotes? 任何换行和双引号的解决方法?


Sample JSON data is as below.. (with double quotes and new line) 样本JSON数据如下..(带有双引号和换行符)

[{"id": "6","title": "We can make iPhone app too now...","detailMessage": "Yes!!! You heard it "right"...

We can make iPhone app too with website....","whenAdd": "11-08-2013"}]

StringEscapeUtils.escapeJavaScript() did the trick. StringEscapeUtils.escapeJavaScript()达到了目的。

Below is the code I have 下面是我的代码

+ "\"detailMessage\": \"" + StringEscapeUtils.escapeJavaScript(rs.getString(3)) + "\","
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM