JavaScript 数组与JSON互相转换

发表在 开发 共有 0 条评论
//数组对象
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']);//啊啊
Responses