2

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

  "({"test1":[{"test2":55,"test":"15.06"},
   {"test3":55,"test4":"15.08"}]})"
3
  • 2
    Apply eval(...) on it. May be also understand the evils of eval. Commented May 1, 2017 at 22:08
  • I see that in that chain you have two objects will always be like this or will it be one? If it's an object you can use JSON.parse Commented May 1, 2017 at 22:12
  • Thanks @KalEl eval() give me object. Commented May 1, 2017 at 22:18

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

3 Comments

thanks @RickHitchcock for this answer is pretty good acctually eval() method solve this problem .
eval() can be dangerous if you don't have control over the string. Otherwise, it should work fine. See stackoverflow.com/questions/86513/…
Ok i undestand thanks for this explanation @RickHitchcock i use your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.