How to convert a JSON-like string to a Javascript object?
I am getting a string like this:
var json = "title: abc, link: http://google.com, size: 1";
How can I convert it to an Javascript object so that I can access it like obj.title
, obj.link
etc. This doesn’开发者_JAVA技巧t work:
var obj = eval("(" + json + ')'); //error
How to achieve this?
Here is the full code
var entry = json.feed.entry[i];
//here entry = title: abc, link: http://google.com, size: 1
entry = entry.content.$t.replace(/: /g, '": "');
entry = entry.replace(/, /g, '", "');
entry = '"' + entry + '"';
var jdata = eval("(" + entry + ')'); //error: missing ) in parenthetical
Use the jQuery.parseJSON() And your json string is not valid! You have to doublequote.
var obj = jQuery.parseJSON('{' + '"title": "abc", "link": "http://google.com", "size": 1' + '}');
精彩评论