网站首页 > 技术文章 正文
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 值。
- 上一篇: Delphi基础教程图文版之JSON
- 下一篇: 只要一个json文件3分钟搭建一个json服务器
猜你喜欢
- 2025-01-13 postman——post请求数据类型
- 2025-01-13 比较一下JSON与XML两种数据格式?
- 2025-01-13 JSON&Ajax介绍和实例
- 2025-01-13 NET Core处理WebAPI JSON返回null为空
- 2025-01-13 jsoncpp使用总结
- 2025-01-13 微信WeUI设计规范文件下载及使用方法
- 2025-01-13 Spring Boot 中使用 JSON Schema 来校验复杂JSON数据
- 2025-01-13 C#解析多层嵌套的JSON数组
- 2025-01-13 web后端正确的返回JSON
- 2025-01-13 3分钟短文 | Linux 使用curl发起post请求的4个常用方式
你 发表评论:
欢迎- 577℃几个Oracle空值处理函数 oracle处理null值的函数
- 573℃Oracle分析函数之Lag和Lead()使用
- 559℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 557℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 554℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 543℃【数据统计分析】详解Oracle分组函数之CUBE
- 531℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 527℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端react (48)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端富文本编辑器 (47)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle的函数 (57)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)