GSON不是对象错误

时间:2013-09-30 16:51:54

标签: javascript json gson

我有以下json对象:

cNGJSON = {
            "one": graph1.graphNode, "two": graph2.graphNode, "three": graph3.graphNode, "four": graph4.graphNode, "five": graph5.graphNode,"six": graph6.graphNode,
            "seven": graph7.graphNode,"eight": graph8.graphNode, "nine": graph9.graphNode,"ten": graph10.graphNode, "eleven": graph11.graphNode, "twelve": graph12.graphNode,
            "thirteen": graph13.graphNode,"fourteen": graph14.graphNode,"fifteen": graph15.graphNode,"sixteen": graph16.graphNode, "seventeen": graph17.graphNode, "eighteen": graph18.graphNode,
            "nineteen": graph19.graphNode   
    };

其中graph1.graphNode是一个整数数组。

[1,2,3,4]

我使用jQuery将其发送到服务器:

  $.ajax({
            url: 'validate',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                console.log("Success!!");
            },
            data: cNGJSON
        });

但是我得到了

Expected BEGIN_OBJECT but was STRING at line 1 column 1

每次尝试都会出错。

我尝试将cNGJSON设置为:

cNGJSON = { 
            "one": "Number one", "two": "Number two"
    };

我仍然得到同样的错误。

1 个答案:

答案 0 :(得分:2)

  

如果要在XHR请求中发送JSON对象,则需要首先对JSON对象进行字符串化

$.ajax({
     url: 'validate',
     type: 'post',
     dataType: 'json',
     success: function (data) {
         console.log("Success!!");
     },
     data: JSON.stringify(cNGJSON)
});