how to solve (Uncaught TypeError: Converting circu

2020-03-08 05:19发布

问题:

i have the following object and i am trying to convert it to json object as follows

 var feeTransactionsArray=[];

                 $(".editor #newPayTable .mainTr").each(function(){ 

                     var feeTransactions={};
                     var studentDetails={};
                     var feeCategory={};


                     studentDetails['studentAdmissionId']=id;

                     feeCategory['feeCatId']=$(this).find('.feeCatId').val();

                     feeTransactions['studentDetails']=studentDetails;

                     feeTransactions['feeCategory']=feeCategory;

                     feeTransactions['paidOn']=paidDate;

                     feeTransactions['transReceiptNo']=receciptNumber;

                     feeTransactions['amountPaid']=$(this).find('.amount').val();

                     feeTransactions['paymentMode']=mode

                     feeTransactions['amountPaid']=refrenceNumber;

                     feeTransactions['isConcessionGiven']='no';

                     feeTransactionsArray.push(feeTransactionsArray);
                 });
                 var myJSON = JSON.stringify(feeTransactionsArray);

this gives following error

actions.js:1180 Uncaught TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)

how to solve this in my case. please help me. thank you!

回答1:

It is not possible to stringify a circual structure in JSON. Lets see a single example:

var a = { a: undefined };
var b = { b: a };
a.a = b;

Then, we have Object:

{ a: { b: { a: { b : { a ... infinite recursion

Which results into the error you described.



回答2:

sorry every one its my silly mistake

it feeTransactionsArray.push(feeTransactionsArray);

should be feeTransactionsArray.push(feeTransactions);