Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

字符串对象怎么转成对象

下面这个typeof是string,请问怎么转成对象

{
    "type":"text",
    "date":1592804746777,
    "SUserId":"24f2c65ec29443fcb20e1acf408fdaf8",
    "content":"56757"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

没理解错的话,你这是一个 JSON 字符串,转成对象只需要:

const str = `
{
    "type":"text",
    "date":1592804746777,
    "SUserId":"24f2c65ec29443fcb20e1acf408fdaf8",
    "content":"56757"
}
`;

// 这是重点,把 JSON 字符串转为对象的方法
const obj = JSON.parse(str);
console.log(typeof obj);  // object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...