【问题标题】:GSON Not an Object ErrorGSON 不是对象错误
【发布时间】:2013-09-30 16:51:54
【问题描述】:

我有以下 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"
    };

我还是遇到同样的错误。

【问题讨论】:

  • 我认为你应该使用jsonData: cNGJSON

标签: javascript json gson


【解决方案1】:

当您想在 XHR 请求中发送 JSON 对象时,您需要先将 JSON 对象字符串化

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

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多