【问题标题】:SyntaxError: Unexpected token [ in JSON at position 1367SyntaxError: Unexpected token [ in JSON at position 1367
【发布时间】:2016-11-03 13:25:25
【问题描述】:

我有一个 AngularJS 前端,它使用 Restangular 通过 Java Servlet 调用后端,返回 JSON 数据,然后使用 Charts.js 对其进行解析以制作图表

角度工厂

app.factory('graphService', function($http, Restangular) {

            var exports = {};

            exports.getRestangular = function() {
                // return Restangular.setBaseUrl("/api");
                return Restangular.setBaseUrl("/apm/graph");
            };

            exports.getGraphDataDC = function(dcName) {
                return exports.getRestangular().all("graphData/DC/" + dcName).getList();
            };
}

Angular UI RestangularCall

graphService.getGraphDataDC(item.name).then(function (data) {
                        $scope.summary = data;
                        $scope.JSONtoArrays(data);
                        console.log(data);
                        $scope.createChart();
                    }, function(data) {
                        //Error
                    });

Java Servlet

try
{
        dcData = persistance.getSummaryDelaysDC80Perc(from, to, pathInfo[pathInfo.length-1]);
        } catch(RuntimeException e) {
        LOGGER.error("Could not load data form database, reason: {}", e);
            throw new ServletException(e);
        }
        // parse to json
    json = ThreadsafeUtils.getGsonInstance(pretty).toJson(dcData);
    LOGGER.debug(dcData.size() + " summary entries were read");

        out.write(json);
        break;
}

Persistence Facade 这是被 Servlet 调用的函数

public List<SummaryDelaysDataCenter80Perc> getSummaryDelaysDC80Perc(Date from, Date to, String dcName)  throws RuntimeException {
        List<SummaryDelaysDataCenter80Perc> result = new ArrayList<>();

        Calendar c = Calendar.getInstance(Locale.GERMANY);
        java.sql.Date minDate;
        java.sql.Date maxDate;
        String call;
        if (from != null)
            minDate = new java.sql.Date(from.getTime());
        else
            minDate = Utils.getDBMinDate();

        if (to != null) {
            maxDate = new java.sql.Date(to.getTime());
        } else {
            maxDate = Utils.getDBMaxDate();
        }
        call = "CALL " + summaryDelaysDCProcedureName + "(?, ?, ?, ?)";

        try {
            //prepare statement
            java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
            java.sql.CallableStatement cst = connection.prepareCall(call);
            //set parameters
            cst.setDate(1, minDate, c);
            cst.setDate(2, maxDate, c);
            cst.setString(3, dcName);
            // execute statement and retrieve result
            cst.execute();
            ResultSet rs = cst.getResultSet();
            while (rs.next()) {
                SummaryDelaysDataCenter80Perc sdeldc80 = new SummaryDelaysDataCenter80Perc();
                DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY);
                String strDate = dateFormat.format(rs.getTimestamp(1));
                sdeldc80.setName(rs.getString(2));
                sdeldc80.setDate(strDate);
                sdeldc80.setPerc80serverdelay(Double.toString(rs.getDouble(3)));
                sdeldc80.setPerc80networkdelay(Double.toString(rs.getDouble(4)));
                sdeldc80.setPerc80clientdelay(Double.toString(rs.getDouble(5)));
                sdeldc80.setPerc80roundtrips(Long.toString(rs.getLong(6)));
                result.add(sdeldc80);
            }
        } catch (java.sql.SQLException e) {
            throw new RuntimeException("StoredProcedureCall was not successful", e);
        }
        return result;

    }

调用getGsonInstance().toJson()后的json变量如下:

[{"Name" : "WER", "Count" : 90, "Date": "2016-05-25" }, 
{"Name" : "TWK", "Count" : 17, "Date": "2016-05-26"  }, 
{"Name" : "XPR", "Count" : 26, "Date": "2016-05-27"  }]

Java Servlet 代码完全执行,但在为$scope.summary 分配检索到的值之前,Unexpected token [ in JSON at position 1367 的代码错误。

JSON 字符串确实有 1367 个字符的大小,但这意味着最后一个位置 1366,所以我不确定为什么它在 1367 处显示错误。

我该如何解决这个错误?

【问题讨论】:

  • 您必须引用Date 值,例如"2016-05-25"
  • @str 很糟糕,我错过了。我刚刚更正了。
  • at position 1367。发布的 JSON 不会出现那么长。
  • 我刚刚发布了 JSON 的样例。
  • 将您的 json 粘贴到 json.parser.online.fr 等工具中。这可以帮助您查看哪个部分无效

标签: javascript java angularjs json servlets


【解决方案1】:

尝试使用 JSON.parse(data)。然后进行其他操作。

【讨论】:

  • 在哪里?在 servlet 中?
  • $scope.summary = JSON.parse(data).
【解决方案2】:

已解决。问题是持久性外观中缺少 break 语句。

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 2019-09-20
    • 2020-10-23
    • 1970-01-01
    • 2022-01-22
    • 2021-09-30
    • 2017-12-15
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多