1

JQuery.parseJSON 関数によって解析されている次の JSON を取得しようとしているときに問題が発生しました。("Uncaught SyntaxError: Unexpected token")

[{"ExtIdremoto":"8","ExtNombre":"Silla bebe","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"13.5","ExtCuantificable":"true"},{"ExtIdremoto":"9","ExtNombre":"Alzador","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"13.5","ExtCuantificable":"false"},{"ExtIdremoto":"10","ExtNombre":"Maxicosi","ExtDescripcion":"Lorem ipsum lorem pete can\r\n•\tBlue\r\n•\tBlue\r\n•\t“blue”","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"},{"ExtIdremoto":"12","ExtNombre":"GPS","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"}]

JSON はhttp://jsonlint.com/検証に合格していますが、「•」が問題を引き起こしているようです。

私はパーサー ライブラリを使用しておらず、ここで指定された関数を使用して解決しようとしています :

どうすればキャラクターをエスケープできますか?

PD: プロジェクトの要件に外部ライブラリを含めないようにしています

アップデート

JSON は文字列に格納され、セッション var を介して jsp に渡されます。

StringBuilder sbJson = new StringBuilder();
sbJson.append("[");
Iterator<DatosExtra> it = datosDisponibilidad.getExtrasOpcionales().iterator();
while(it.hasNext()){
    DatosExtra datos = (DatosExtra) it.next();
    sbJson.append("{\"ExtIdremoto\":").append("\"").append(datos.getExtIdremoto()).append("\"").append(",\"ExtNombre\":").append(escaparCaracteresJSON(datos.getExtNombre(locale)))
                    .append(",\"ExtDescripcion\":").append(escaparCaracteresJSON(datos.getExtDescripcion(locale)))
                    .append(",\"ExtPrecioDia\":").append("\"").append(datos.getExtPrecioDia()).append("\"")
                    .append(",\"ExtPrecio\":").append("\"").append(datos.getExtPrecio()).append("\"")
                    .append(",\"ExtCuantificable\":").append("\"").append("S".equals(datos.getExtCuantificable())).append("\"")
                    .append("}");
    if(it.hasNext()) sbJson.append(",");
}
sbJson.append("]");
hpRequest.getRequest().getSession().setAttribute("jsonExtras", sbJson.toString());

escaparCaracteresJSON はコメント化された前の関数です

JSP で値を復元します。

var jsonExtras = jQuery.parseJSON('<%=session.getAttribute("jsonExtras")%>');

出力はまさにこれです:

var jsonExtras = jQuery.parseJSON('[{"ExtIdremoto":"8","ExtNombre":"Silla bebe","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"9","ExtCuantificable":"true"},{"ExtIdremoto":"9","ExtNombre":"Alzador","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"4.5","ExtPrecio":"9","ExtCuantificable":"false"},{"ExtIdremoto":"10","ExtNombre":"Maxicosi","ExtDescripcion":"Lorem ipsum lorem pete can\r\n•\tBlue\r\n•\tBlue\r\n•\t“blue”","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"},{"ExtIdremoto":"12","ExtNombre":"GPS","ExtDescripcion":"Lorem ipsum lorem pete can","ExtPrecioDia":"0","ExtPrecio":"0","ExtCuantificable":"true"}]');
4

1 に答える 1