Restroing Array Children values via JSON
This question already has an answer here:
这并不好,但如果你想自动做这件事,我认为你找不到更好的方法。
// String with values
var s = '[1,2,3,4,5,6]';
// Expected variables
var a = ('xx,yy,zz,aa,bb,cc,dd,ee').split(',');
// Changing string in String[]
var m = s.match(/d+/g);
a.forEach(function (v, i) {
// Extract value.
var n = m[i];
// Prevent change empty value.
if(n)
// Set value by evaluation (Be careful, this variable must be in scope!).
// eval is EVIL!
eval('(' + v + '=' + n + ')');
});
如果你的变量是全局定义的,你可以尝试
var variablenames = ["xx", "yy"];
var variables = [xx,yy];
for (var i=0; i<variables.length; i++) {
window[variablenames[i]] = variables[i];
}
可能是这样的:
var variables = {};
variables.xx = 100;
variables.yy = 200;
var jsonObj = JSON.stringify(variables); // converted to Json object
console.log(jsonObj); // outputs: {"xx":100,"yy":200}
var obj1 = JSON.parse(jsonObj);
alert(obj1.xx + " " + obj1.yy); // alerts (100 200);
链接地址: http://www.djcxy.com/p/8094.html
上一篇: JSON字符串到JS对象
下一篇: 通过JSON解析Array儿童值