JavaScript 数组与JSON互相转换

2018-05-05 23:11:02
//数组对象
var index ={'name1':'啊啊','name2':'哈哈'};
console.log(typeof(index));//object
console.log(index);//{name1: "啊啊", name2: "哈哈"}
console.log(index['name2']);//哈哈
console.log(index.name2);//哈哈

//转为JSON字符串
var nn=JSON.stringify(index);
console.log(typeof(nn));//string
console.log(nn);//{"name1":"啊啊","name2":"哈哈"}
console.log(nn['name1']);//undefined
console.log(nn.name1);//undefined

//转为JavaScript对象
var dd=JSON.parse(nn);
console.log(typeof(dd));//object
console.log(dd);//{name1: "啊啊", name2: "哈哈"}
console.log(dd.name1);//啊啊
console.log(dd['name1']);//啊啊
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »