无效的JSON,意外的令牌

Invalid JSON,Unexpected Token

本文关键字:令牌 意外 JSON 无效      更新时间:2023-09-26

我正在尝试使用angular发送JSON数据到服务器,但是得到JSON。

有时工作,有时抛出错误。

我认为这是因为我用来创建一些密钥的时间戳。

{
    "genericformfieldId": "1",
    "userId": "2",
    "formData": {
        "_1443551400000": [
            {
                "mValue": "HARYANA",
                "type": "DropDown",
                "name": "selectState"
            }
        ],
        "_1443637800000": [
            {
                "mValue": "CHHATTISGARH",
                "type": "DropDown",
                "name": "selectState"
            }
        ],
        "_1443810600000": [
            {
                "mValue": "sac",
                "type": "SingleLineText",
                "name": "departureFrom"
            }
        ]
    }
}

请建议。

添加数据发布代码

$http({
    method: 'POST',
    url:    Url, 
    headers: { "Content-Type": "application/json" }, 
    data: formData
    })
.success( function( response, status, headers, config ) {
    console.log( response );
    if( response ) {                    
        deferred.resolve( response );
    }
})
.error( function( response, status, headers, config ) {  
    deferred.reject( null );
});

如果JSON。解析一个对象,抛出"Unexpected token o"仅仅是因为您试图解析object.toString(),它是[object object]。Try to JSON.parse('[object Object]'); ;)

这个应该可以用

var data = '{
    "genericformfieldId": "1",
    "userId": "2",
    "formData": {
        "_1443551400000": [
            {
                "mValue": "HARYANA",
                "type": "DropDown",
                "name": "selectState"
            }
        ],
        "_1443637800000": [
            {
                "mValue": "CHHATTISGARH",
                "type": "DropDown",
                "name": "selectState"
            }
        ],
        "_1443810600000": [
            {
                "mValue": "sac",
                "type": "SingleLineText",
                "name": "departureFrom"
            }
        ]
    }
}';
JSON.parse(data);

这个答案https://stackoverflow.com/a/12719860/1868660解释了Unexpected token ILLEGAL(…)问题

必须清理输入json。

检查:https://jsfiddle.net/am190cv5/

来源:

var s = '{"genericformfieldId": "1","userId": "2","formData": {"_1443551400000": [{"mValue": "HARYANA","type": "DropDown","name": "selectState"}],"_1443637800000": [{"mValue": "CHHATTISGARH","type": "DropDown","name": "selectState"}],"_1443810600000": [{"mValue": "sac","type": "SingleLineText","name": "departureFrom"}]}}';
var result = JSON.parse(s);
console.log(result);

打开控制台,查看结果