Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/ainto.org/usr/plugins/TpCache/driver/typecho_mysql.class.php on line 93
JavaScript 数组与JSON互相转换 - Ainto

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']);//啊啊
评论已关闭