1. JSON 对象 → JSON 字符串

JSON.stringify()

1
2
3
4
5
6
7
8
const obj = {
id: '0',
name: 'Jack',
age: '18',
};

const str = JSON.stringify(obj);
console.log(str); // {"id":"0","name":"Jack","age":"18"}

2. JSON 字符串 → JSON 对象

JSON.parse()

1
2
3
4
const str = '{"id":"0","name":"Jack","age":"18"}';

const obj = JSON.parse(str);
console.log(obj);