I have this String response from a JSP:
{"status":"ok","tipo":"orden","ordenes":"[{"numero":"15056","fecha":"2006-03-28","proveedor":"101","codigo":"15","orden":"5","fepago":"2006-03-29","marca":"1","razon":"XXXXXXX","importe":"1500.0"}]"}
When I try to parse this result to JSON, I receive this error:
Uncaught SyntaxError: Unexpected token n in JSON at position 44
This is how I do stringify the JSON (no, I cannot use any JSON libraries):
String ordenObtenida = "{\"status\":\"ok\",\"tipo\":\"" + tipo + "\",\"ordenes\":\"[";
while (rs.next()) {
hasRow = true;
if (rs.getString("razon") != null) {
razon = rs.getString("razon").replaceAll("\"", "").trim();
}
ordenObtenida += "{\"numero\":\"" + rs.getInt("numero") + "\",\"fecha\":\"" + rs.getDate("fecha") + "\",\"proveedor\":\"" + rs.getInt("proveedor") + "\","
+ "\"codigo\":\"" + rs.getInt("codigo") + "\",\"orden\":\"" + rs.getInt("orden") + "\",\"fepago\":\"" + rs.getDate("fepago") + "\",\"marca\":\"" + rs.getInt("marca") + "\","
+ "\"razon\":\"" + razon + "\",\"importe\":\"" + rs.getFloat("importe") + "\"},";
}
ordenObtenida = ordenObtenida.substring(0, ordenObtenida.length() - 1) + "]\"}";
And this is how I parse it (with jQuery):
$.ajax({
type: 'POST',
url: 'TraePorOrden.jsp',
data: dato
}).success(function (msg) {
var msg = $.trim(msg);
//msg = JSON.stringify(msg);
var js = $.parseJSON(msg);
});
If I uncomment the line msg = JSON.stringify(msg);, the JSON parses correctly but all the attributes are undefined.
Please advise.
String ordenObtenida) is wrong:"ordenes\":\"[". Shouldn't it be"ordenes\":["?