Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

JSON.parse()

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

JSON.parse() 메소드는 JSON을 문자열로 파싱하며, 파싱된 값을 추가로 변환하기도 합니다.

Syntax

JSON.parse(text[, reviver])

인자값

text
JSON으로 파싱할 문자열. JSON 문법에 대해선 JSON 오브젝트를 참조하세요.
reviver Optional
함수가 올 경우, 리턴되기 전에 파싱된 문자열 값이 어떻게 변환될 것인지를 결정함.

리턴

주어진 JSON 텍스트에 따라 Object를 리턴함

에러

파싱할 문자열이 유효한 JSON이 아닐 경우 SyntaxError  예외를 던짐.

예제

JSON.parse() 사용하기

JSON.parse('{}');              // {}
JSON.parse('true');            // true
JSON.parse('"foo"');           // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');            // null

reviver 인자값 사용하기

reviver 가 주어지면,  파싱된 값이 리턴되기 전에 변환됩니다. 구체적으로 말하면,  파싱되는 값과 그 값의 프로퍼티들(가장 멀리 떨어진 프로퍼티 부터 시작하여 본래 파싱되는 값까지 거슬러감)은 각기 reviver에 의해 처리됩니다. reviver는 this로 선언된 프로퍼티와 프로퍼티의 이름 그리고 프로퍼티의 값이 포함된 오브젝트와 함께 호출됩니다. 만약 reviver  함수가  undefined를 반환 하거나 반환 값 없이 함수 실행이 종료되면 해당 프로퍼티는 반횐되는 오브젝트로 부터 삭제됩니다. 그외의 경우에는 변환되어 반환됩니다.

The reviver is ultimately called with the empty string and the topmost value to permit transformation of the topmost value. Be certain to handle this case properly, usually by returning the provided value, or JSON.parse() will return undefined.

JSON.parse('{"p": 5}', function(k, v) {
  if (k === '') { return v; } // if topmost value, return it,
  return v * 2;               // else return v * 2.
});                           // { p: 10 }

JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', function(k, v) {
  console.log(k); // log the current property name, the last is "".
  return v;       // return the unchanged property value.
});

// 1
// 2
// 4
// 6
// 5
// 3 
// ""

JSON.parse() does not allow trailing commas

// both will throw a SyntaxError
JSON.parse('[1, 2, 3, 4, ]');
JSON.parse('{"foo" : 1, }');

Specifications

Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'JSON.parse' in that specification.
Standard Initial definition. Implemented in JavaScript 1.7.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'JSON.parse' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'JSON.parse' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 3.5 (1.9.1) 8.0 10.5 4.0
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) 1.0 (1.0) (Yes) (Yes) (Yes)

Gecko-specific notes

Starting Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26), a malformed JSON string yields a more detailed error message containing the line and column number that caused the parsing error. This is useful when debugging large JSON data.

JSON.parse('[1, 2, 3, 4,]');
// SyntaxError: JSON.parse: unexpected character at
// line 1 column 13 of the JSON data

See also

문서 태그 및 공헌자

 이 페이지의 공헌자: subasuba
 최종 변경: subasuba,