2

私のブラウザ(chrome)は私のJSON(ASP.NETの戻り値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.parseJSON 文字列を JavaScript オブジェクトに解析するためのものです。

オブジェクト(質問に投稿したもの)を解析しようとすると、Chrome で「予期しないトークン」メッセージが表示されます。

したがって、変換する必要のないものを変換しようとするか、オブジェクトを JSON に変換する必要があります。stringify

于 2012-08-24T19:36:29.127 に答える
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>
于 2012-08-24T19:47:10.720 に答える
1

フュージョン テーブルを使用してアプリをビルドするときに、同じエラーが発生しました。Google からの応答が正しく解析されていませんでした。ネイティブ json ライブラリをjson2.jsに置き換えたところ、問題は解決しました。

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

于 2012-10-01T12:24:22.830 に答える
0

簡単な JSON 試行で (Chrome で) この問題に遭遇しました:

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

$.parseJSON で同じエラーが発生しました。

私が見つけた回避策は、それを配列で囲むことです (そして、インデックス 0 を参照します)。

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