js之json对象与字符串相互转换
2019-04-24 本文已影响0人
贤总_
一.对象转字符串 JSON.stringify()
const obj = {
id: 1,
name: 'jessie-xian',
age: 18
}
const objToStr = JSON.stringify(obj)
console.log('obj:', obj)
console.log('objToStr:', objToStr)
二.字符串转对象 JSON.parse()
const str = '{"id":1,"name":"jessie-xian","age":18}'
const strToObj = JSON.parse(str)
console.log('str:', str)
console.log('strToObj:', strToObj)