JS里的数组无法转换成JSON格式
<scripttype="text/javascript">varchooselist=newArray();/*chooselist['112233']="100001...
<script type="text/javascript">
var chooselist = new Array();
/*
chooselist['112233']="1000011,1000022,1000033";
chooselist['445566']="1000044,1000055,1000066";
chooselist['778899']="1000077,1000088,1000099";
*/
chooselist.push({'112233':'1000011,1000022,1000033'});
chooselist.push({'445566':'1000044,1000055,1000066'});
chooselist.push({'778899':'1000077,1000088,1000099'});
<script type="text/javascript">
var chooselist = new Array();
//下面的数组拼装方式不能转成JSON,而我需要的就是这种
/*
chooselist['112233']="1000011,1000022,1000033";
chooselist['445566']="1000044,1000055,1000066";
chooselist['778899']="1000077,1000088,1000099";
*/
//这种把数组压栈方式倒是能转化成JSON,但是不是我不想要的
chooselist.push({'112233':'1000011,1000022,1000033'});
chooselist.push({'445566':'1000044,1000055,1000066'});
chooselist.push({'778899':'1000077,1000088,1000099'});
// datas['suggest']=chooselist;
// alert(chooselist);
var datas1 = $.toJSON(chooselist);
function test(){
alert(datas1);
$.ajax({
type: "POST",
url: "__ROOT__/ajaxApp/testfun",
data:{"name":datas1},
datatype: "json",
success:function(ret){
var jsonobj = $.parseJSON(ret);
alert(jsonobj.msg)
}
}
);
}
</script> 展开
var chooselist = new Array();
/*
chooselist['112233']="1000011,1000022,1000033";
chooselist['445566']="1000044,1000055,1000066";
chooselist['778899']="1000077,1000088,1000099";
*/
chooselist.push({'112233':'1000011,1000022,1000033'});
chooselist.push({'445566':'1000044,1000055,1000066'});
chooselist.push({'778899':'1000077,1000088,1000099'});
<script type="text/javascript">
var chooselist = new Array();
//下面的数组拼装方式不能转成JSON,而我需要的就是这种
/*
chooselist['112233']="1000011,1000022,1000033";
chooselist['445566']="1000044,1000055,1000066";
chooselist['778899']="1000077,1000088,1000099";
*/
//这种把数组压栈方式倒是能转化成JSON,但是不是我不想要的
chooselist.push({'112233':'1000011,1000022,1000033'});
chooselist.push({'445566':'1000044,1000055,1000066'});
chooselist.push({'778899':'1000077,1000088,1000099'});
// datas['suggest']=chooselist;
// alert(chooselist);
var datas1 = $.toJSON(chooselist);
function test(){
alert(datas1);
$.ajax({
type: "POST",
url: "__ROOT__/ajaxApp/testfun",
data:{"name":datas1},
datatype: "json",
success:function(ret){
var jsonobj = $.parseJSON(ret);
alert(jsonobj.msg)
}
}
);
}
</script> 展开
1个回答
展开全部
可以考虑用JSON.stringify方法
代码如下
var chooselist = new Array();
chooselist['112233']="1000011,1000022,1000033";
chooselist['445566']="1000044,1000055,1000066";
chooselist['778899']="1000077,1000088,1000099";
var json = {};
for(var key in chooselist){
json[key]=chooselist[key];
}
//输出 值如{"112233":"1000011,1000022,1000033","445566":"1000044,1000055,1000066","778899":"1000077,1000088,1000099"}
console.log(JSON.stringify(json));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询