我如何将该字符串转换为对象,以便可以访问obj.Name
之类的属性?
{
Name = Mahbubr Rahman,
Gender = Male,
Birthday = 1 / 5 / 1992 6: 00: 00 AM,
Email = mahbubur.rahman@ rms.com,
EmployeeType = Manager
}
我尝试使用
JSON.parse()
和eval
,但一无所获。有什么帮助吗?var obj = JSON.parse(
JSON.stringify('{ Name = Mahbubr Rahman,Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = [email protected], EmployeeType = Manager }'.replace(/=/g, ':'))
);
最佳答案
也尝试这个
var str = "{ Name = Mahbubr Rahman,Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = [email protected], EmployeeType = Manager }"
var obj = JSON.parse(str.split(/\s*=\s*/).join("\":\"").split(/\s*,\s*/).join("\",\"").split(/{\s*/).join("{\"").split(/\s*}/).join("\"}"));
console.log(obj);