开发者

Having problems turning JSON into javascript object

I'm using jquery and the jquery-json plugin found here: http://code.google.com/p/jquery-json/

I've checked at h开发者_StackOverflow社区ttp://jsonlint.com/ to make sure the returned JSON is valid and it is.

I cannot get this function in the jquery-json plugin to return a javascript object. It simply throws an error at JSON.parse(src) on line 118 of the plugin (un-minified).

var data = $.evalJSON(rsp);
console.log(data);

Where rsp is the response from $.ajax() success callback. Also,

typeof rsp // returns object


The $.ajax method will parse the JSON for you, so you don't have to do that. What's sent to the success callback is already an object.

Also, if you are using jQuery 1.4.1 or later, you don't need a plugin if you would need to parse JSON. It has the $.parseJSON method built in.


If rsp is already an Object then you don't have to parse it. If typeof rsp returned String then you would.


I've always just used javascript's built in eval() function: eval('('+rsp+')'); seeing as JSON is literally just a javascript object. (Hence its name: Javascript Object Notation)


rsp is already a javascript object, since typeof rsp == 'object'. You don't need to do anything to it.

If it was a JSON-encoded string, then typeof rsp == 'string', and only then would you need $.evalJSON.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜