如何JSON字符串化到JavaScript数组 [英] How to stringify JSON to JavaScript array
问题描述
我在HTML DOM的形式是一个复选框,单击(可以有多个)。在当过我使用单引号的描述字符串时,会出现问题,因为我的列表对象是单引号deliniated。这是形式的复选框之一:
My form in the html DOM is a checkbox to click (there can be more than one). The problem occurs in the description string when ever I use an apostrophe, since my list object is single-quote deliniated. This is one of the checkboxes in the form:
<input type="checkbox" id="cbx" name="cbx" value="{'getPic': 'url', 'picsrc': 'http://lh3.ggpht.com/_ZB3cttqooN0/SVmJPfusGWI/AAAAAAAADvA/GuIRgh6eMOI/Grand%20Canyon%201213_121508214.JPG', 'pos': None, 'description': 'Here's what it REALLY looks like at 5:30am! Bring your headlight!'}">
这读取选中的复选框的值,并将其推到一个数组(列表)的JavaScript:
The javascript that reads the values of the checked checkboxes and pushes them into an array (list):
var pylist = [];
for (i=0; i<document.picList.cbx.length; i++) {
if (document.picList.cbx[i].checked) {
pylist.push( document.picList.cbx[i].value );
}
}
var getstr = JSON.stringify(pylist);
问题始终是getstr在这一点上的说明财产的单引号之后砍掉了一切。
我已经试过了逃逸到一点用不同的方式。
The problem is always that getstr at this point has chopped off everthing after the single quote in the description property. I've tried different ways of escaping it to little avail.
推荐答案
的问题是,该复选框的值已经是一个JSON字符串。一个解决办法是对值调用JSON.parse()来:
The problem is that the value of the checkbox already is a JSON string. One solution would be to call JSON.parse() on the value:
var pylist = [];
for (i=0; i<document.picList.cbx.length; i++) {
if (document.picList.cbx[i].checked) {
pylist.push( JSON.parse( document.picList.cbx[i].value) );
}
}
var getstr = JSON.stringify(pylist);
这篇关于如何JSON字符串化到JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!