[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse [Back]  [Original]

JSON.parse() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JSON.parse()

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

* Some parts of this feature may have varying levels of support.

JSON.parse() JSON JavaScript reviver

In this article

js
JSON.parse(text[, reviver])

text

JSON JSON JSON

reviver

JSON (function)

JSON text Object

JSON SyntaxError

JSON.parse()

js
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 reviver undefined

reviver

js
JSON.parse('{"p": 5}', function (k, v) {
  if (typeof v === "number") {
    return v * 2; // return v * 2 for numbers
  }
  return v; // return everything else unchanged
});

// { 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()

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

Specification
ECMAScript 2027 LanguageSpecification
# sec-json.parse

Gecko

Gecko 29 JSON JSON

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


Web Proxy Viewer  |  New URL  |  Original Page