1

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"}]');
Community
  • 1
  • 1
GLlompart
  • 231
  • 5
  • 18
  • No, that works fine for me. Are you sure you're not doing something that might strip the backslashes? Like, did you write that JSON into your JS as a string or so? – thejh Jun 10 '13 at 08:31
  • Yes, I forgot to put that. The JSON is builded in a stringbuilder and stored in a session attribute that is printed out after in a JS var: var jsonExtras = jQuery.parseJSON(''); – GLlompart Jun 10 '13 at 08:32

1 Answers1

2

I believe you are missing one backslash in \r, \n and \t - it should be \\r, \\n and \\t. Adding those makes jQuery.parseJSON() work just fine - http://jsfiddle.net/Nv282/. Could you show how are you escaping characters in escaparCaracteresJSON() function?

dimchez
  • 1,974
  • 1
  • 14
  • 9
  • The function is this one http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json/16652683#16652683 – GLlompart Jun 10 '13 at 08:59
  • @GLlompart This function escapes control characters and makes it a valid string in Java - `'\r'` character becomes `"\r"` string. Since you are trying to use this string in Javascript you need an extra backslash to properly escape control characters. Update your `escaparCaracteresJSON()` function to append `"\\\\n"` instead of `"\\n"` (similarly for other control characters as well). The idea is to have double backslash for each control character in the output string. – dimchez Jun 10 '13 at 09:30