0

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.

6
  • can you paste your response that you see in Chrome Developer for example ? Commented Jun 8, 2017 at 18:22
  • 4
    I think your string (when you initialize String ordenObtenida) is wrong: "ordenes\":\"[". Shouldn't it be "ordenes\":["? Commented Jun 8, 2017 at 18:23
  • This is not a JAVA or JSP question. The client does not care where the JSON came from. Commented Jun 8, 2017 at 18:26
  • 1
    Don't create JSON by concatenating strings. Java has APIs to create JSON. If you paste your JSON into a jsfiddle and click "Tidy" you can see the format is messed up. Commented Jun 8, 2017 at 18:29
  • @ChrisG I don't think he's allowed to use them: "(no, i can't use JSONObject)" Commented Jun 8, 2017 at 18:30

1 Answer 1

2

The response coming from your JSP is not a valid JSON, first of all. You are including quotes around ordenes array, it should be like this:

{
  "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"
    }
  ]
}

I'm not a specialist on Java but on the JSON making function, you're sending ordenes array as a string.

My suggestion for you is to don't do string concatenation and use a Java library for JSON creation. Also, try to remove the quotes around the array response, like below.

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) + "]\"}";
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, like @Ishnark says, that correct. Whit respect to JSON library, i can't use it, but i know how it's work (a lor more easier)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.