How can I convert a JSON string to an object in JavaScript? Is there a method that does this?
Something like:
var x = "{ id: 5, name: 'hello' }";
var y = /*something*/(x);
alert(y.id + " " + y.name);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As per the comments and question history it look like that you’re already using jQuery. In that case, it’s good to know that jQuery ships with a new
parseJSON()function since version 1.4.1 which was released late January this year. Consider upgrading if you’re not at 1.4.1 yet. Here’s an extract of relevance from its API documentation: