jQuery JSON.parse 到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9484474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:52:11  来源:igfitidea点击:

JSON.parse to array

jqueryjson

提问by John

I have a json object:

我有一个 json 对象:

var deniedTimeIDs = JSON.parse('[808,809,812,811,814,815]');

so, I want to add/remove data from this object by jquery. How to do it? can I convert it to Array? Thanks

所以,我想通过 jquery 从这个对象中添加/删除数据。怎么做?我可以将其转换为数组吗?谢谢

回答by Selvakumar Arumugam

Below will give you a javascript object,

下面会给你一个javascript对象,

var deniedTimeIDs = JSON.parse('[808,809,812,811,814,815]');

You can then use .push& .popto add/remove element into the array.

然后您可以使用.push& .pop将元素添加/删除到数组中。

deniedTimeIDs.push(100); //will result in [808,809,812,811,814,815,100]

Further Readings,

进一步阅读,

JSON.parse, Array.push, Array.pop

JSON.parse, Array.push, Array.pop

回答by Umesh Patil

Any Array returned after parsing the String, can be processed with jQuery or JavaScript. We generally use Push() and Pop() function to process any array.

解析字符串后返回的任何 Array 都可以使用 jQuery 或 JavaScript 进行处理。我们通常使用 Push() 和 Pop() 函数来处理任何数组。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
    var deniedTimeIDs = JSON.parse('[808,809,812,811,814,815]');
    // You can use push/Pop to remove the IDs from an array.
    console.log(deniedTimeIDs);// o/p=> [808,809,812,811,814,815]
    //You can iterate this array using jQuery.
    $.each(deniedTimeIDs,function(key,val){
        console.log(val); 
    })
});
</script>

回答by Diode

var deniedTimeIDs = $.parseJSON('[808,809,812,811,814,815]');

回答by udidu

If you want to parse that String and represent it as an Array you can do the following:

如果您想解析该字符串并将其表示为一个数组,您可以执行以下操作:

// Warning: eval is weird
var arr = eval('[808,809,812,811,814,815]');

or

或者

var arr= JSON.parse('[808,809,812,811,814,815]');

Now arris a valid JavaScript array.

现在arr是一个有效的 JavaScript 数组。