2013-06-10 7 views
1

JQuery.parseJSON 함수로 다음 JSON을 파싱하는 중 문제가 발생했습니다. ("catch되지 않은 구문 에러 : 예기치 않은 토큰")parseJSON이 유효한 JSON에 오류를 발생시킵니다.

[{"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으로는 문자열로 저장되며, JSP로 세션 VAR 통과에 대한 모든 외부 라이브러리를 포함한다.

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"}]'); 
+0

아니, 그게 잘 작동합니다. 백 슬래시를 벗겨 낼 수있는 일을하지 않았습니까? 마찬가지로 JSON을 JS에 문자열로 작성 했습니까? – thejh

+0

네, 잊어 버렸습니다. JSON은 stringbuilder로 빌드되고 JS에서 출력되는 세션 속성에 저장됩니다. var : var jsonExtras = jQuery.parseJSON ('<% = session.getAttribute ("jsonExtras") %>'); – GLlompart

답변

2

내가 당신을 믿지 escaparCaracteresJSON는 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()); 

나는 값을 복구 \r에 하나의 백 슬래시가 누락되었습니다. \n\t - \\r, \\n\\t이어야합니다. 그것들을 추가하면 jQuery.parseJSON()이 잘 작동합니다 - http://jsfiddle.net/Nv282/. escaparCaracteresJSON()의 문자를 어떻게 피할 수 있습니까?

+0

이 함수는이 함수입니다. http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json/16652683#16652683 – GLlompart

+0

@GLlompart이 함수는 제어 문자를 이스케이프 처리하고 유효한 문자열로 만듭니다. 자바 -' '\ r' '문자는 ""\ r "'문자열이됩니다. Javascript에서이 문자열을 사용하려고하므로 제어 문자를 올바르게 이스케이프 처리하려면 추가 백 슬래시가 필요합니다. 'escaparCaracteresJSON()'함수를 업데이트하여''\\ n "'대신에''\\\\ n"'을 추가하십시오 (다른 제어 문자와 마찬가지로). 출력 문자열의 각 제어 문자에 대해 이중 백 슬래시를 사용하는 것이 좋습니다. – dimchez

+0

문제를 해결해 주셔서 감사합니다. – GLlompart

관련 문제