I'm having trouble while trying to get the following JSON being parsed by JQuery.parseJSON function. ("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"}]
Despite that the JSON passes the http://jsonlint.com/ validation it looks like the "•" is causing the problem.
I'm not using any parser library and I've been trying to work it out with the function specified here: https://stackoverflow.com/a/16652683/1161355
How can I scape the character?
PD: I'm trying to avoid include any external library for project requirements
Update
The JSON is stored into a string and passed through session var to the 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());
Where escaparCaracteresJSON is the commented previous function
At the JSP I recover the value:
var jsonExtras = jQuery.parseJSON('<%=session.getAttribute("jsonExtras")%>');
The output is exactly this one:
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"}]');