如何解决错误:从JSON阅读器读取时发现了一个意外的'StartArray'节点.期望一个'StartObject'节点.

Tim*_*son 2 javascript ajax json dynamics-crm asp.net-web-api

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

从JSON阅读器读取时发现了一个意外的'StartArray'节点.期望一个'StartObject'节点.

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

我看过这个问题和这个问题,但没有成功.

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

变量

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";
Run Code Online (Sandbox Code Playgroud)

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");
            }
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

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;
 }
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 7

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

我相信你需要改变的是:

toJSON(): any[] {
     let json = [];
     json[0] = {
         //...
Run Code Online (Sandbox Code Playgroud)

toJSON(): any[] {
     let json = {
        //...
Run Code Online (Sandbox Code Playgroud)