2

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

  "({"test1":[{"test2":55,"test":"15.06"},
   {"test3":55,"test4":"15.08"}]})"
Nasirli Teymur
  • 53
  • 1
  • 10

1 Answers1

7

If you remove the surrounding parentheses, you will get a JSON string, which can be converted to an object using 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);
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
  • thanks @RickHitchcock for this answer is pretty good acctually eval() method solve this problem . – Nasirli Teymur May 01 '17 at 22:24
  • 2
    `eval()` can be dangerous if you don't have control over the string. Otherwise, it should work fine. See http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Rick Hitchcock May 01 '17 at 22:26
  • Ok i undestand thanks for this explanation @RickHitchcock i use your code. – Nasirli Teymur May 01 '17 at 22:32