2

我的浏览器(chrome)似乎不喜欢我的 JSON(通过 ASP.NET 的 return Json(object) 格式化)。

我失败的 JavaScript 处理程序如下所示:

var responseObj = JSON.parse(msg);

在我解析它之前,我可以看到 chrome JavaScript 调试器中的所有内容,但是当我将它发送到 JSON.parse() 时它失败了。

真正奇怪的是 JSONLint 说它是有效的。

我的 JSON 看起来像这样(实际对象要长得多,信不信由你):

{
    "crct": {
        "ETL_ID": 221,
        "DELETE_FLAG": "N",
        "ID": "a0Wc00000008sZGEAY",
        "ISDELETED": "false",
        "NAMEX": "CRCT-4474",
        "CREATEDDATE": "/Date(1345769442000)/",
        "CREATEDBYID": "005C00000049nuYIAQ",
        "LASTMODIFIEDDATE": "/Date(1345771729000)/",
        "LASTMODIFIEDBYID": "005C00000049nuYIAQ",
        "SYSTEMMODSTAMP": "/Date(1345771729000)/",
        "CLIENT_TASK": "a0Xc0000001lLihEAE",
        "COUNTRY": null,
        "PRA_CRCT_ID": null,
        "PROJECT_REGION": "a0mc00000008ZPgAAM",
        "TOTAL_UNITS_FRM": 200,
        "UNIT_SCOPE_CHANGE": null,
        "UNIT_SCOPE_DIFFERENCE": 0,
        "CUMULATIVE_OFFICIAL_FORECAST_U": 200,
        "CUMULATIVE_PENDING_DOWNSCOPE_U": 0,
        "CUMULATIVE_BASELINE_UNITS_ROLL": 200,
        "CUMULATIVE_WORKED_HOURS_ROLLUP": 0,
        "CUMULATIVE_WORKED_UNITS_ROLLUP": 0,
        "CLIENT_UNIT_NUMBER": null,
        "EntityState": 2,
        "EntityKey": {
            "EntitySetName": "SF_CLIENT_REGION_COUNTRY_TASK",
            "EntityContainerName": "SFEntities",
            "EntityKeyValues": [
                {
                    "Key": "ETL_ID",
                    "Value": 221
                }
            ],
            "IsTemporary": false
        }
    },
    "unitChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    },
    "effortChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    },
    "hourChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    },
    "effortRatioChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    }
}
4

4 回答 4

3

JSON.parse用于将 JSON 字符串解析为 JavaScript 对象。

如果您尝试解析一个对象(这是您在问题中发布的内容),您将在 Chrome 中收到“意外令牌”消息。

所以,要么你试图转换不需要转换的东西,要么你想把一个对象转换成 JSON,在这种情况下你需要stringify

于 2012-08-24T19:36:29.127 回答
1

没有看到你的代码,很难说发生了什么,但你的 JSON 很好:

现在,如果您尝试在 Javascript 编码中使用它,那么您不应该使用 JSON,而应该只分配一个对象。不需要解析,编译器会为您完成。

换句话说——不要做我刚才做的事。我只是用它来表明它可以在 Chrome (21.0.1180.57) 下正确解析为 JSON

<html>
<head>
<title>Quick test</title>

<script>
var a = '\
{\
    "crct": {\
        "ETL_ID": 221,\
        "DELETE_FLAG": "N",\
        "ID": "a0Wc00000008sZGEAY",\
        "ISDELETED": "false",\
        "NAMEX": "CRCT-4474",\
        "CREATEDDATE": "/Date(1345769442000)/",\
        "CREATEDBYID": "005C00000049nuYIAQ",\
        "LASTMODIFIEDDATE": "/Date(1345771729000)/",\
        "LASTMODIFIEDBYID": "005C00000049nuYIAQ",\
        "SYSTEMMODSTAMP": "/Date(1345771729000)/",\
        "CLIENT_TASK": "a0Xc0000001lLihEAE",\
        "COUNTRY": null,\
        "PRA_CRCT_ID": null,\
        "PROJECT_REGION": "a0mc00000008ZPgAAM",\
        "TOTAL_UNITS_FRM": 200,\
        "UNIT_SCOPE_CHANGE": null,\
        "UNIT_SCOPE_DIFFERENCE": 0,\
        "CUMULATIVE_OFFICIAL_FORECAST_U": 200,\
        "CUMULATIVE_PENDING_DOWNSCOPE_U": 0,\
        "CUMULATIVE_BASELINE_UNITS_ROLL": 200,\
        "CUMULATIVE_WORKED_HOURS_ROLLUP": 0,\
        "CUMULATIVE_WORKED_UNITS_ROLLUP": 0,\
        "CLIENT_UNIT_NUMBER": null,\
        "EntityState": 2,\
        "EntityKey": {\
            "EntitySetName": "SF_CLIENT_REGION_COUNTRY_TASK",\
            "EntityContainerName": "SFEntities",\
            "EntityKeyValues": [\
                {\
                    "Key": "ETL_ID",\
                    "Value": 221\
                }\
            ],\
            "IsTemporary": false\
        }\
    },\
    "unitChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    },\
    "effortChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    },\
    "hourChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    },\
    "effortRatioChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    }\
}';

var b = JSON.parse(a);
window.alert(typeof b);
window.alert(JSON.stringify(b));

</script>
<body>
This is a test
</body>
</html>
于 2012-08-24T19:47:10.720 回答
1

在构建带有融合表的应用程序时,我遇到了同样的错误。谷歌的回复没有被正确解析。我用json2.js替换了原生的 json 库,问题就解决了。

https://github.com/douglascrockford/JSON-js

于 2012-10-01T12:24:22.830 回答
0

我通过简单的 JSON 尝试遇到了这个问题(使用 Chrome):

var TOC = { id: 1, title: "header" }

$.parseJSON 给出了同样的错误。

我发现的解决方法是将它包含在一个数组中(然后引用索引零)。

var TOC = [{ id: 1, title: "header" }][0]
于 2013-03-14T02:18:46.080 回答