我的浏览器(chrome)似乎不喜欢我的JSON(通过ASP.NET返回Json(对象)格式化)。
我失败的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
}
}
答案 0 :(得分:3)
JSON.parse
用于将JSON字符串解析为JavaScript对象。
如果您尝试解析一个对象(这是您在问题中发布的内容),您将在Chrome中收到“意外令牌”消息。
因此,要么尝试转换不需要转换的内容,要么想要将对象转换为JSON,在这种情况下,您需要stringify
答案 1 :(得分:1)
如果没有看到您的代码,很难说会发生什么,但您的JSON很好:
现在,如果您尝试在Javascript编码中使用此,那么您不应该使用JSON,而应该只是分配一个对象。不需要解析,编译器会为您执行此操作。
换句话说 - 不要做我刚刚做的事情。我只是用它来显示作为JSON 它会在Chrome下正确解析(21.0.1180.57)<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>
答案 2 :(得分:1)
在使用融合表构建应用程序时,我遇到了同样的错误。来自谷歌的回复没有被正确解析。我用json2.js替换了原生的json库,问题解决了。
答案 3 :(得分:0)
我通过简单的JSON尝试遇到了这个问题(使用Chrome):
var TOC = { id: 1, title: "header" }
$。parseJSON给出了同样的错误。
我找到的解决方法是将它包含在一个数组中(然后引用索引零)。
var TOC = [{ id: 1, title: "header" }][0]