parseJSON将在有效的JSON上抛出错误

时间:2013-06-10 08:23:14

标签: java jquery json java-ee encoding

我在尝试通过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/验证,但看起来“•”导致问题。

我没有使用任何解析器库,我一直在尝试使用此处指定的函数来解决它: https://stackoverflow.com/a/16652683/1161355

我如何看待角色?

PD:我正在努力避免为项目要求包含任何外部库

更新

JSON存储在一个字符串中,并通过session 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"}]');

1 个答案:

答案 0 :(得分:2)

我相信你错过了\r\n\t中的一个反斜杠 - 它应该是\\r\\n\\t。添加这些内容可使jQuery.parseJSON()正常工作 - http://jsfiddle.net/Nv282/。你能说明如何在escaparCaracteresJSON()函数中转义字符吗?