javascript - 使用jquery从JSP响应中解析Json

标签 javascript java jquery json jsp

我有来自 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"}]"}

当我尝试将此结果解析为 JSON 时,收到此错误:

Uncaught SyntaxError: Unexpected token n in JSON at position 44

这就是我对 JSON 进行字符串化的方法(不,我不能使用任何 JSON 库):

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

这就是我解析它的方式(使用 jQuery):

$.ajax({
    type: 'POST',
    url: 'TraePorOrden.jsp',
    data: dato
}).success(function (msg) {
    var msg = $.trim(msg);
    //msg = JSON.stringify(msg);
    var js = $.parseJSON(msg);
});

如果我取消注释行 msg = JSON.stringify(msg);,JSON 会正确解析,但所有属性都未定义

请指教。

最佳答案

首先,来自 JSP 的响应不是有效的 JSON。 您在 ordenes 数组周围包含引号,它应该是这样的:

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

我不是 Java 方面的专家,但在 JSON 制作功能上,您将 ordenes 数组作为字符串发送。

我对您的建议是不要进行字符串连接并使用 Java 库来创建 JSON。另外,尝试删除数组响应周围的引号,如下所示。

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

关于javascript - 使用jquery从JSP响应中解析Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442944/

相关文章:

javascript - document.execCommand 在 Firefox 和 IE 中不起作用

java - 在运行时创建 JLabel : View in Swing does not update as intended

java - 在 Hortonworks 沙箱中流式传输 Hadoop jar 文件,无 contrib 目录

php - 根据 PHP url 变量显示特定模式

jQuery UI 组合框空值渲染

javascript - HTML5 文件上传进度 - 仅限客户端

javascript - 避免下载 pdf 和图像

java - 使用 Flowable 在 Spring 应用程序中进行集成测试

jquery - ASP.NET MVC 页面中多个 $(document).ready(function(){... 的触发顺序

javascript - 如何在 jquery 中使用 php while 循环中生成的唯一 ID