Convert array to JSON

后端 未结 9 1535
南笙
南笙 2020-11-22 04:28

I have an Array var cars = [2,3,..] which holds a few integers. I\'ve added a few values to the array, but I now need to send this array to a page via jQuery\'s

9条回答
  •  灰色年华
    2020-11-22 04:38

    I decided to use the json2 library and I got an error about “cyclic data structures”.

    I got it solved by telling json2 how to convert my complex object. Not only it works now but also I have included only the fields I need. Here is how I did it:

    OBJ.prototype.toJSON = function (key) {
           var returnObj = new Object();
           returnObj.devid = this.devid;
           returnObj.name = this.name;
           returnObj.speed = this.speed;
           returnObj.status = this.status;
           return returnObj;
       }
    

提交回复
热议问题