gpt4 book ai didi

Javascript:将字符串转换为对象

转载 作者:行者123 更新时间:2023-11-30 12:06:18 25 4
gpt4 key购买 nike

如何将此字符串转换为对象,以便我可以访问它的属性,如 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 = mahbubur.rahman@rms.com, EmployeeType = Manager }'.replace(/=/g, ':'))
);

最佳答案

也试试这个

var str = "{ Name = Mahbubr Rahman,Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = mahbubur.rahman@rms.com, EmployeeType = Manager }"

var obj = JSON.parse(str.split(/\s*=\s*/).join("\":\"").split(/\s*,\s*/).join("\",\"").split(/{\s*/).join("{\"").split(/\s*}/).join("\"}"));

console.log(obj);

关于Javascript:将字符串转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com