0

I receive a string in the format given below (with double quotes around it):

 "'{ "param" : "value"}'"

How can I convert this string into json object so that I could use it as

alert(obj.param)

Thank you


Edit

As I mentioned in above the string has double quotes around it. Here is an example of how I get this string

CSHTML

@{
    var prop = "{ \"param\" : \"value\"}";
    <a data-type="@prop"></a>
}

and in JS I have this

var obj = anchor.data('type');

I want to be able to use obj.param, how do I achieve this?

Thanks

somedude
  • 169
  • 3
  • 12
  • have you seen this http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – Sasa Sep 25 '14 at 22:10
  • Please check here for more details: http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – gus Sep 25 '14 at 22:17

1 Answers1

4

Use JSON.parse it parses a string as a JSON

var obj = JSON.parse(json);
Isaac
  • 983
  • 1
  • 7
  • 13
  • @csharp_beginner replace `var obj = anchor.data('type');` by `var obj = JSON.parse(prop);` – Isaac Sep 25 '14 at 22:42