js之json对象与字符串相互转换

Posted on 2019-09-23 14:35  jessie-xian  阅读(134)  评论(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)