The special implementation of Json.parse in Chrome _json

Source: Internet
Author: User
IE8/FIREFOX3.5+/CHROME4/SAFARI4/OPERA10 has implemented this method. The way to use it is simple:
Copy Code code as follows:

var str = ' {' name ': ' Jack '} ';
var json = json.parse (str);
alert (json.name);

"Jack" will pop up in the browser that implements the method above.
If you add a method of parsing JSON to Object.prototype (someone may be strongly opposed to doing so polluting the native object, here for purely discussion)
Copy Code code as follows:

Object.prototype.parseJSON = function () {
Return Json.parse (this);
}

Because all objects inherit the method of object, this can be used directly,
Copy Code code as follows:

var str = ' {' name ': ' Jack '} ';
var json = Str.parsejson ();
alert (json.name);

When Str.parsejson (), the inside of Parsejson points to Str. Not all browsers can parse success at this time.

Ie8/firefox/safari/opera still pops up "Jack", and Chrome complains: uncaught illegal access.
Why does chrome not support this? Compare two ways, pass to json.parse argument one is String str, one is this. Looks like these two are no different?
When Str.parsejson (), the inside of the Parsejson points to Str. Modify the Parsejson method:
Copy Code code as follows:

Object.prototype.parseJSON = function () {
Alert (typeof this);
Return Json.parse (this);
};

Re-executing, you can find that the Parsejson pop-up is object, maybe this is the difference. Direct new A string will see the obvious effect.
Copy Code code as follows:

var js = json.parse (new String (' {' name ': ' Jack '} '));
alert (js.name);

The above code in addition to Chrome error, other browsers are performing normally.
Basically come to the conclusion:
In Chrome, the first argument for Json.parse can only be a string, not an object (including new string or unsupported)
Then go back and add a method of parsing JSON to Object.prototype, and if you want to be compatible with all browsers, you can write this:
Copy Code code as follows:

Object.prototype.parseJSON = function () {
Return Json.parse (This.tostring ());
}
var str = ' {' name ': ' Jack '} ';
var json = Str.parsejson ();
alert (json.name);

2010-10-09: The bug has been fixed in Chrome6.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.