js生成json数据

时间:2024-03-11 09:31:59

<script src="~/static/js/jquery.min.js"></script>
<script type="text/javascript">
var list = [];//创建数组
var a1 = {};//创建对象
a1.t = "a";
a1.v = "b,s,g";
var a2 = {};
a2.t = "c";
a2.v = "d,h,e";
list.push(a1);//添加对象
list.push(a2);

var j = JSON.stringify(list);//将对象转换为json
console.log(j);

alert(j);

//将字符串转换为json对象

JSON.parse(\'[{"t":"a","v":"b,s,g"},{"t":"c","v":"d,h,e"}]\');
</script>