将字符串转换为对象javascript / jquery

使困惑
Id: {
    editable: false,
    nullable: true
},
CustomerID: {
    editable: true,
    type: Integer
},
CompanyName: {
    editable: true,
    type: String
},
ContactName: {
    editable: true,
    type: String
}

嗨,在javascript或jquery中,有没有一种方法可以将这个String转换为Object?

谢谢。

系统发育

如果严格来说这是字符串的格式,则它是无效的JSON。但是,可以明智地使用来解决此问题eval()

eval('myobj = {' + stringifiedObject + '};');

为了澄清起见,您不能JSON.parse()在此处使用的原因是JSON规范要求所有键都加引号。

JSON.parse('{ ID: 1 }');   // Generates an exception: Invalid character
JSON.parse('{ "ID": 1 }'); // Returns object: { ID: 1 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章