如何解决错误:意外的' StartArray'从JSON阅读器读取时找到了节点。 A' StartObject'节点是预期的。?

时间:2017-04-04 15:05:13

标签: javascript json ajax asp.net-web-api dynamics-crm

我正在尝试使用Web API向Dynamics 2016 On Premise添加实体。实体引用其他三个实体。当我尝试发布实体时,收到以下错误。

  

意想不到的' StartArray'从JSON阅读器读取时找到了节点。 A' StartObject'节点是预期的。

我引用的三个实体发生此错误:ccseq_clientid,ccseq_employeeid,ccseq_setid。

我查看了this问题和this问题,但没有成功。

此错误意味着什么,我该如何解决?

变量

public contentType: string = "application/json;";
public dataType: string = "json";
public expenseTransaction: string = this.connection + "ccseq_expensetransactions";
public expenseTransactionSet: string = this.connection + "ccseq_expensetransactionsets";
public client: string = this.connection + "ccseq_clients";
public systemUser: string = this.connection + "systemusers";

Ajax Call

Create(expenses: Array<ExpenseTransactionBase>): void {
    for (let i: number = 0; i < expenses.length; i++) {
        $.ajax({
            url: this.expenseTransaction,
            type: "POST",
            contentType: this.contentType,
            accepts: this.contentType,
            dataType: this.dataType,
            data: JSON.stringify(expenses[i].toJSON()),
            success: function (data: any): void {
                alert("Success");
            },
            error: function (data: any) {
                alert("error");
            }
        });
    }
};

JSON创建

toJSON(): any[] {
     let json = [];
     json[0] = {
         "ccseq_clientid@odata.bind": this.client + "(" + this.ccseq_clientid + ")",
         "ccseq_clientnumber": this.ccseq_clientnumber,
         "ccseq_employeefirstname": this.ccseq_employeefirstname,
         "ccseq_employeelastname": this.ccseq_employeelastname,
         "ccseq_employeeid@odata.bind": this.systemUser + "(" + this.ccseq_employeeid + ")",
         "ccseq_expenseerrorcode": this.ccseq_expenseerrorcode,
         "ccseq_generalledgername": this.ccseq_generalledgername,
         "ccseq_generalledgernumber": this.ccseq_generalledgernumber,
         "ccseq_groupcode": this.ccseq_groupcode,
         "ccseq_mastercardposteddate": this.ccseq_mastercardposteddate,
         "ccseq_mastercardtransactiondate": this.ccseq_mastercardtransactiondate,
         "ccseq_mastercardtransactionparentcompany": this.ccseq_mastercardtransactionparentcompany,
         "ccseq_navcompanycode": this.ccseq_navcompanycode,
         "ccseq_navemployeeid": this.ccseq_navemployeeid,
         "ccseq_navgeographycode": this.ccseq_navgeographycode,
         "ccseq_navjobclasscode": this.ccseq_navjobclasscode,
         "ccseq_navservicecode": this.ccseq_navservicecode,
         "ccseq_setid@odata.bind": this.expenseTransactionSet + "(" + this.ccseq_setid + ")",
         "ccseq_transactionamount": this.ccseq_transactionamount,
         "ccseq_transactiondate": this.ccseq_transactiondate,
         "ccseq_transactiondescription": this.ccseq_transactiondescription,
         "ccseq_transactiontype": this.ccseq_transactiontype,
         "ccseq_vendor": this.ccseq_vendor,
         "statecode": this.statecode
     };

     return json;
 }

1 个答案:

答案 0 :(得分:6)

如错误中所述,您应该POST一个JSON对象,而不是数组。

我相信你需要改变的是:

toJSON(): any[] {
     let json = [];
     json[0] = {
         //...

toJSON(): any[] {
     let json = {
        //...