无法解析来自服务器的JSON数据

时间:2012-02-29 05:45:40

标签: java jquery json datatables

我收到“DataTables警告:无法解析来自服务器的JSON数据。这是由JSON格式错误引起的” 我知道这可能是其他线程的重复,我在我的代码中使用java和dataTables。我以JSON格式获取输出但在打印时无法在客户端识别。

当我在http://jsonlint.com/中验证JSON对象时,其格式正确。我不确定是什么导致了这个错误。

  

{ “iTotalRecords”:5 “sEcho”: “1”, “aaData”:[[ “V2993ASFKH230943”, “本田”, “协议”],[ “V2993A39SNF30943”, “本田”, “CRV”] ,[ “V4833A39SNF30943”, “极品”, “TSX”],[ “V4833RE9SNF30943”, “极品”, “TL”],[ “V9383RE9SNF30943”, “极品”, “MDX”]] “iTotalDisplayRecords”:5}

任何帮助都会非常感激。

由于

修改

try {
            JSONObject jsonResponse = new JSONObject();

            jsonResponse.put("sEcho", sEcho);
            jsonResponse.put("iTotalRecords", iTotalRecords);
            jsonResponse.put("iTotalDisplayRecords", iTotalDisplayRecords);

            while (itr.hasNext()) {
                Map rs = (Map) itr.next();
                FleetEquipment eqpmt = new FleetEquipment();
                eqpmt.setVinNumber((String) rs.get("VIN_ID"));
                eqpmt.setMake((String) rs.get("MAKE"));
                eqpmt.setModel((String) rs.get("MODEL"));
                JSONArray row = new JSONArray();
                row.put(eqpmt.getVinNumber()).put(eqpmt.getMake())
                        .put(eqpmt.getModel());
                data.put(row);

            }

            jsonResponse.put("aaData", data);

            response.setContentType("application/json");
            response.getWriter().print(jsonResponse.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response.setContentType("text/html");
            response.getWriter().print(e.getMessage());
        }

编辑1

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<portlet:defineObjects/>

<portlet:resourceURL var="listURL" id="list" escapeXml="false"/>
<script type="text/javascript">
$(document).ready(function () {
            $('#listTable').dataTable({
                "bServerSide": true,
                "sAjaxSource": "<%=listURL.toString()%>",
                "bProcessing": true,
                "sPaginationType": "full_numbers",
                "bJQueryUI": true
            });
        });

</script></head>
<body>

<table id="listTable">
    <thead>
        <tr>
            <th>VIN ID</th>
            <th>MAKE</th>
            <th>MODEL</th>
            <th>MODEL1</th>
            <th>Model2</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

2 个答案:

答案 0 :(得分:2)

enter image description here [不是问题的答案,不过是工具输入]

http://jsonviewer.stack.hu/

此工具确实有助于理解外部JSON对象的结构。这可能不是你的问题的答案,但是,这有助于我理解和解析它与python代码,它也是一个JSON验证器。

答案 1 :(得分:1)

\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t\r\n\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\r\n\t\r\n\t\r\n\t\t\r\n\t\r\n VIN ID MAKE MODEL ","
status ":200,"
statusText ":"
OK "},"
fnServerData ":"
function (e, j, l, k) {\n k.jqXHR = i.ajax({
        url: e,
        data: j,
        success: function (e) {
            i(k.oInstance).trigger(\"xhr\", k);l(e);}, dataType: \"json\", cache: !1, type: k.sServerMethod, error: function (e, i) {\"parsererror\" == i && alert(\"DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.\");}});\n}", "aoServerParams": [],
            "sServerMethod": "GET",
            "fnFormatNumber": "function (e) {\n if (1000 > e) {\n return e;\n }\n for (var i = e + \"\", e = i.split(\"\"), j = \"\", i = i.length, l = 0; l < i; l++) {\n 0 === l % 3 &&\n 0 !== l && (j = this.oLanguage.sInfoThousands + j), j = e[i - l - 1] + j;\n }\n return j;\n}",
            "aLengthMenu": [10, 25, 50, 100],
            "iDraw": 1,
            "bDrawing": true,
            "iDrawError": -1,
            "_iDisplayLength": 10,
            "_iDisplayStart": 0,
            "_iDisplayEnd": 0,
            "_iRecordsTotal": 0,
            "_iRecordsDisplay": 0,
            "bJUI": true,
            "oClasses": {
                "sTable": "dataTable",
                "sPagePrevEnabled": "fg-button ui-button ui-state-default ui-corner-left",
                "sPagePrevDisabled": "fg-button ui-button ui-state-default ui-corner-left ui-state-disabled",
                "sPageNextEnabled": "fg-button ui-button ui-state-default ui-corner-right",
                "sPageNextDisabled": "fg-button ui-button ui-state-default ui-corner-right ui-state-disabled",
                "sPageJUINext": "ui-icon ui-icon-circle-arrow-e",
                "sPageJUIPrev": "ui-icon ui-icon-circle-arrow-w",
                "sPageButton": "fg-button ui-button ui-state-default",
                "sPageButtonActive": "fg-button ui-button ui-state-default ui-state-disabled",
                "sPageButtonStaticDisabled": "fg-button ui-button ui-state-default ui-state-disabled",
                "sPageFirst": "first ui-corner-tl ui-corner-bl",
                "sPagePrevious": "previous",
                "sPageNext": "next",
                "sPageLast": "last ui-corner-tr ui-corner-br",
                "sStripeOdd": "odd",
                "sStripeEven": "even",
                "sRowEmpty": "dataTables_empty",
                "sWrapper": "dataTables_wrapper",
                "sFilter": "dataTables_filter",
                "sInfo": "dataTables_info",
                "sPaging": "dataTables_paginate fg-buttonset ui-buttonset fg-buttonset-multi ui-buttonset-multi paging_",
                "sLength": "dataTables_length",
                "sProcessing": "dataTables_processing",
                "sSortAsc": "ui-state-default",
                "sSortDesc": "ui-state-default",
                "sSortable": "ui-state-default",
                "sSortableAsc": "ui-state-default",
                "sSortableDesc": "ui-state-default",
                "sSortableNone": "ui-state-default",
                "sSortColumn": "sorting_",
                "sSortJUIAsc": "css_right ui-icon ui-icon-triangle-1-n",
                "sSortJUIDesc": "css_right ui-icon ui-icon-triangle-1-s",
                "sSortJUI": "css_right ui-icon ui-icon-carat-2-n-s",
                "sSortJUIAscAllowed": "css_right ui-icon ui-icon-carat-1-n",
                "sSortJUIDescAllowed": "css_right ui-icon ui-icon-carat-1-s",
                "sSortJUIWrapper": "DataTables_sort_wrapper",
                "sSortIcon": "DataTables_sort_icon",
                "sScrollWrapper": "dataTables_scroll",
                "sScrollHead": "dataTables_scrollHead ui-state-default",
                "sScrollHeadInner": "dataTables_scrollHeadInner",
                "sScrollBody": "dataTables_scrollBody",
                "sScrollFoot": "dataTables_scrollFoot ui-state-default",
                "sScrollFootInner": "dataTables_scrollFootInner",
                "sFooterTH": "ui-state-default"
            },
            "bFiltered": true,
            "bSorted": true,
            "bSortCellsTop": false,
            "oInit": {
                "bServerSide": true,
                "sAjaxSource": "/wps/myportal/!ut/p/b1/hc09DoJAEIbhs3CCGX5kl3IRdVGiolFkG7Mho2IQEkAKTy8aGwt0ukmeLy8oSE3PctAeWY4LB1Cl7vKzbvOq1MXrV-6RBz73fVMg9-QUw0j6bCLmiGj2IO3BeCakwyJEPuImhkLumLsNLBT2v30C6psgrlhP7Jlki72Fof0BvxJvgAMnEJayulGfShUbDMUISU1Nda8zgs2pIGqjvGkhznR2oYg6Ktb6THBThRde2UMIw3gC131G1Q!!/",
                "bProcessing": true,
                "sPaginationType": "full_numbers",
                "bJQueryUI": true
            },
            "aoDestroyCallback": [],
            "fnRecordsTotal": "function () {\n return this.oFeatures.bServerSide ? parseInt(this._iRecordsTotal, 10) : this.aiDisplayMaster.length;\n}",
            "fnRecordsDisplay": "function () {\n return this.oFeatures.bServerSide ? parseInt(this._iRecordsDisplay, 10) : this.aiDisplay.length;\n}",
            "fnDisplayEnd": "function () {\n return this.oFeatures.bServerSide ? !1 === this.oFeatures.bPaginate || - 1 == this._iDisplayLength ? this._iDisplayStart + this.aiDisplay.length : Math.min(this._iDisplayStart + this._iDisplayLength, this._iRecordsDisplay) : this._iDisplayEnd;\n}",
            "sInstance": "fleetList",
            "iTabIndex": 0,
            "fnStateLoad": "function (e) {\n var e = this.oApi._fnReadCookie(e.sCookiePrefix + e.sInstance), j;\n try {\n j = \"function\" === typeof i.parseJSON ? i.parseJSON(e) : eval(\"(\" + e + \")\");\n } catch (l) {\n j = null;\n }\n return j;\n}",
            "fnStateSave": "function (e, i) {\n this.oApi._fnCreateCookie(e.sCookiePrefix + e.sInstance, this.oApi._fnJsonString(i), e.iCookieDuration, e.sCookiePrefix, e.fnCookieCallback);\n}",
            "iInitDisplayStart": -1,
            "nTableReinsertBefore": null
            }],
        "tablesDisplayData": [
            []
        ],
        "tablesDisplayExtra": [
            []
        ]
    }
    };