在Javascript中将字符串转换为对象

时间:2022-09-01 22:25:09

How can I convert a string to object? that is my data :

如何将字符串转换为对象?这是我的数据:

  "({"test1":[{"test2":55,"test":"15.06"},
   {"test3":55,"test4":"15.08"}]})"

1 个解决方案

#1


7  

If you remove the surrounding parentheses, you will get a JSON string, which can be converted to an object using JSON.parse():

如果删除了周围的圆括号,就会得到一个JSON字符串,可以使用JSON.parse()将其转换为对象:

var s = '({"test1":[{"test2":55,"test":"15.06"}, {"test3":55,"test4":"15.08"}]})',
    j = s.replace(/^\((.+)\)$/, '$1'),  //remove surrounding parentheses
    o = JSON.parse(j);

console.log(o);

#1


7  

If you remove the surrounding parentheses, you will get a JSON string, which can be converted to an object using JSON.parse():

如果删除了周围的圆括号,就会得到一个JSON字符串,可以使用JSON.parse()将其转换为对象:

var s = '({"test1":[{"test2":55,"test":"15.06"}, {"test3":55,"test4":"15.08"}]})',
    j = s.replace(/^\((.+)\)$/, '$1'),  //remove surrounding parentheses
    o = JSON.parse(j);

console.log(o);