jQuery.parseJSON 대 JSON.parse
jQuery.parseJSON
과 JSON.parse
같은 작업을 수행하는 두 가지 기능은 다음과 같습니다. jQuery 라이브러리가 이미로드 된 경우 성능면에서를 jQuery.parseJSON
사용하는 것보다을 사용하는 것이 더 낫 JSON.parse
습니까?
그렇다면 그 이유는 무엇입니까? 아니라면 왜 안됩니까?
다음은 jQuery 1.9.1에서 발췌 한 것입니다 .
parseJSON: function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
if ( data === null ) {
return data;
}
if ( typeof data === "string" ) {
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );
if ( data ) {
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
.replace( rvalidtokens, "]" )
.replace( rvalidbraces, "")) ) {
return ( new Function( "return " + data ) )();
}
}
}
jQuery.error( "Invalid JSON: " + data );
},
당신이 볼 수 있듯이, jQuery를 네이티브 사용 JSON.parse
가능한 경우 방법을, 그렇지 않으면 그것은과 데이터 평가하려고합니다 new Function
종류 등이다 eval
.
네, 확실히 jQuery.parseJSON
.
브라우저가 JSON.parse의 기본 구현을 제공하는 경우 jQuery는이를 사용하여 문자열을 구문 분석합니다.
따라서 브라우저에 기본 구현이없는 경우 jQuery가 JSON 파서를 제공함을 의미합니다. 다음 은 JSON 기능이 있거나없는 브라우저 의 비교 차트 입니다.
JSON.parse ()는 기본적으로 다른 브라우저가 아닌 일부 브라우저에서 사용할 수 있으므로 라이브러리를 사용하는 것이 더 안전합니다. 다른 응답자들이 언급했듯이 JQuery 구현은 잘 작동합니다. 도 있습니다 더글라스 크록 포드의 JSON 라이브러리 를 사용할 경우 기본 구현을 사용합니다.
JSON 라이브러리는 JavaScript 객체를 현재 jQuery에서 누락 된 JSON 문자열로 변환하는 방법이 있다는 장점이 있습니다.
jQuery 버전 3 (2016 년에 출시됨)을 사용하는 경우 가 deprecatedJSON.parse()
되었기 때문에 사용해야합니다 .jQuery.parseJSON()
jQuery 3.0부터 $ .parseJSON은 더 이상 사용되지 않습니다. JSON 객체를 구문 분석하려면 대신 기본 JSON.parse 메서드를 사용합니다.
성능에 대해서는 모르지만 ie7 이하와 같은 일부 브라우저에는 기본적으로 JSON 기능이 없을 수 있으므로 jQuery 메서드를 사용하는 것이 확실히 더 안전합니다. 반복을 위해
배열의 기본 forEach
메소드 대신 jQuery의 각 메소드를 사용하는 것처럼 호환성에 관한 것입니다 .
성능 에 대해 말하면 가장 최근의 답변은 JSON.parse
입니다.
기본 JSON 객체는 요즘 모든 브라우저 에서 지원 되므로 . 여기에서 지원 테이블을 볼 수 있습니다 : http://caniuse.com/#feat=jsonJSON.parse
You can also search for this alias appearances at JQuery's repository on GitHub: https://github.com/jquery/jquery/search?utf8=%E2%9C%93&q=parseJSON
Also, jQuery.parseJson
was deprecated on version 3.0+ as mentioned by other answers here.
You should only use jQuery's version if you're an old JQuery version + if you want to provide support for very old browsers (normally, not recommended).
jQuery internally uses JSON.parse
to parse the JSON file.So it doesn't make any difference in most cases.
But some of the older browsers don't support JSON.parse
functionality.In that case using jQuery.parseJSON
is beneficial as jQuery can handle JSON using its own function.
NOTE:
jQuery.parseJSON
is deprecated from jQuery 3.0.So please use the nativeJSON.parse
method.
참고URL : https://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse
'your programing' 카테고리의 다른 글
qDebug, qWarning, qCritical 등 출력을 리디렉션하는 방법은 무엇입니까? (0) | 2020.10.14 |
---|---|
[]로 값을 추가하기 전에 PHP 배열을 선언해야합니까? (0) | 2020.10.14 |
github 페이지 용 저장소를 두 개 이상 만들 수 있나요? (0) | 2020.10.14 |
Android에서 홈 및 기타 시스템 버튼을 비활성화하는 방법은 무엇입니까? (0) | 2020.10.14 |
xcode5에서 전체 빌드 로그를 보려면 어떻게합니까? (0) | 2020.10.14 |