1、javascript把二维数组转为json格式的字符串代码如下:
// 二维数组转为json格式的字符串
function arrayToJsonString(o) {
var len = o.length;
var new_arr = new Array();
var str = '',strone='',strs='',jsonstr='';
for(var i = 0;i<len;i++){
new_arr = o[i];
for(var k in new_arr){
strone += '"'+k+'"'+':'+'"'+new_arr[k]+'"'+',';
}
str = '{'+strone.substring(0,strone.length-1)+'}';
strone='';
strs += str+',';
new_arr=[];
}
strs = '['+strs.substring(0,strs.length-1)+']';
return strs;
}
2、改变多维数组中某个元素的值,代码如下:
var sku_att_json_arr = new Array({$sku_att_json|raw});
sku_att_json_arr[0][0]['quantity'] = 123;
$('#sku_att_json').val(arrayToJsonString(sku_att_json_arr[0]));