Convert array to JSON

后端 未结 9 1523
南笙
南笙 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 04:47

    I made it that way:

    if I have:

    var jsonArg1 = new Object();
        jsonArg1.name = 'calc this';
        jsonArg1.value = 3.1415;
    var jsonArg2 = new Object();
        jsonArg2.name = 'calc this again';
        jsonArg2.value = 2.73;
    
    var pluginArrayArg = new Array();
        pluginArrayArg.push(jsonArg1);
        pluginArrayArg.push(jsonArg2);
    

    to convert pluginArrayArg (which is pure javascript array) into JSON array:

    var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
    

提交回复
热议问题