领先的免费Web技术教程,涵盖HTML到ASP.NET

网站首页 > 知识剖析 正文

JSON.parse 的6大特性

nixiaole 2024-11-18 15:30:41 知识剖析 71 ℃

JSON

JSON的key具有唯一性


JSON.parse()

概念

MDN 文档对它的解释如下:

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


翻译过来大概是:

JSON.parse() 方法解析 JSON 字符串,构造字符串描述的 JavaScript 值或对象。 可以提供一个可选的 reviver 函数来在结果对象返回之前对其执行转换。


基础语法

JSON.stringify(value[, reviver])

// 句法
JSON.parse(text)
JSON.parse(text, reviver)

第一个参数 value (必需的)

要解析为 JSON 的字符串。


特性一

value 是有效的 JSON 字符串,并转化为对应的对象或值。

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

console.log(obj.count);
// expected output: 42

console.log(obj.result);
// expected output: true




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

特性二

JSON.parse() 不允许尾随逗号。

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

特性三

JSON.parse() 不允许单引号,或 JSON 对象的 key 没有引号。

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

特性四

JSON 字符串中的值如果是 undefined,NaN、Infinity、Symbol、函数、布尔值、数字、字符串的包装对象时,将会报语法错误。

// will throw a SyntaxError
JSON.parse('[{"foo": undefined}]');

JSON.parse('{"foo": undefined}');
JSON.parse('{"foo": NaN}');
JSON.parse('{"foo": function(){return true}}');
JSON.parse('{"foo": Symbol("dd")}');
JSON.parse('{"foo": new Number(1)}')

JSON.parse('{"foo": new Number(1)}')
JSON.parse('{"foo": String("false")}')
JSON.parse('{"foo": new Boolean(false)}')


第二个参数reviver (可选的)

特性一

如果是函数,则它规定了在返回之前如何 转换 最初由解析产生的值。

JSON.parse('{"p": 5, "q":"qq"}', (key, value) =>
  typeof value === 'number'
    ? value * 2 // return value * 2 for numbers
    : value     // return everything else unchanged
);
// { p: 10, q: 'qq' }

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

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


特性二

如果 reviver 指定了 a ,则解析计算的值 在返回之前进行转换。具体来说,计算值及其所有属性(从最嵌套的属性开始,一直到原始值本身)都单独通过 reviver。然后调用它,包含被处理的属性的对象为this,属性名称为字符串,属性值作为参数。如果 reviver 函数返回 undefined(或不返回值,例如,如果执行落在函数末尾),则从对象中删除该属性。否则,该属性被重新定义为返回值。

console.log(JSON.parse('{"p": 5, "q": "qq"}', (key, value) =>
  typeof value === 'number'
    ? undefined
    : value
));
// { q: 'qq' }

console.log(JSON.parse('{"p": 5, "q": "qq"}', (key, value) =>
  typeof value === 'number'
    ? null
    : value
));
// { p: null, q: 'qq' }

console.log(JSON.parse('{"p": 5, "q": "qq"}', (key, value) =>
  typeof value === 'number'
    ? 1
    : value
));
// { p: 1, q: 'qq' }

console.log(JSON.parse('{"p": 5, "q": "qq"}', (key, value) =>
  typeof value === 'number'
    ? this
    : value
));
// { p: {}, q: 'qq' }


返回值

表示给定 JSON 文本对应的 Object、Array、string、number、boolean 或 null 值。

Tags:

最近发表
标签列表