一、JSON.stringify() 方法-对象或值转换为 JSON 字符串。
const obj = { name: "shuai", age: 25, city: "New York" };
// 转换为 JSON 字符串
const jsonString = JSON.stringify(obj);
console.log(jsonString); // 输出:'{"name":"shuai","age":25,"city":"New York"}'
二、JSON.parse()方法-将JSON字符串转换为JavaScript对象。
const obj = '{"name":"shuai","age":25,"city":"New York"}';
// 转换为 JSON 字符串
const jsonString = JSON.parse(obj);
console.log(jsonString); // 输出:{ name: "shuai", age: 25, city: "New York" }