JavaScript JSON parse() 메서드
❮
JSON 객체
❯
예시
문자열(JSON 형식으로 작성)을 구문 분석하고 JavaScript 객체를 반환합니다.
var obj = JSON.parse('{"firstName":"John", "lastName":"Doe"}');
아래에서 더 많은 "직접 사용해 보기" 예를 살펴보세요.
정의 및 사용
JSON.parse() 메서드는 문자열을 구문 분석하고 JavaScript 객체를 반환합니다.
문자열은 JSON 형식으로 작성해야 합니다.
JSON.parse() 메서드는 선택적으로 함수를 사용하여 결과를 변환할 수 있습니다.
브라우저 지원
표의 숫자는 이 방법을 완전히 지원하는 첫 번째 브라우저 버전을 지정합니다.
Method | |||||
---|---|---|---|---|---|
parse() | 4.0 | 8.0 | 3.5 | 4.0 | 11.5 |
통사론
JSON.parse(string, function)
매개변수 값
Parameter | Description |
---|---|
string | Required. A string written in JSON format |
reviver function | Optional. A function used to transform the result. The function is called for each item. Any nested objects are transformed before the parent.
|
기술적 세부 사항
반환 값: | JSON 객체 또는 배열 |
---|---|
자바스크립트 버전: | ECMA스크립트 5 |
더 많은 예
예시
리바이버 기능 을 사용하는 방법 :
/*replace the value of "city" to upper case:*/
var text =
'{ "name":"John", "age":"39", "city":"New York"}';
var obj = JSON.parse(text, function (key, value) {
if
(key == "city") {
return value.toUpperCase();
} else {
return value;
}
});
document.getElementById("demo").innerHTML = obj.name + ", " + obj.city;
예시
서버에서 받은 JSON 구문 분석:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200) {
var myObj =
JSON.parse(this.responseText);
document.getElementById("demo").innerHTML
= myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
json_demo.txt 를 살펴보십시오.
관련 페이지
JSON 튜토리얼: JSON 소개
❮
JSON 객체
❯