Convert array to JSON

后端 未结 9 1513
南笙
南笙 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:52

    because my array was like below: and I used .push function to create it dynamically

    my_array = ["234", "23423"];
    

    The only way I converted my array into json is

    json = Object.assign({}, my_array);
    
    0 讨论(0)
  • 2020-11-22 04:54

    One other way could be this:

            var json_arr = {};
            json_arr["name1"] = "value1";
            json_arr["name2"] = "value2";
            json_arr["name3"] = "value3";
    
            var json_string = JSON.stringify(json_arr);
    
    0 讨论(0)
  • 2020-11-22 04:57

    If you have only 1 object like the one you asked, the following will work.

    var x = [{'a':'b'}];
    var b= JSON.stringify(x);
    var c = b.substring(1,b.length-1);
    JSON.parse(c); 
    
    0 讨论(0)
提交回复
热议问题