[Javascript] JSON.parse API
JSON
(JavaScript Object Notation) is a standard method to serialize JavaScript objects and is commonly used to transfer data from the server to the browser. The browser has a JSON
API that allows you to parse the JSON
string into a JavaScript object. This API allows you to customize the parsing behavior very specifically as well.
If you get a string which represent an json object, you can use JSON.parse to parse the string:
var input = '[{"id":1,"title":"Gone with the Wind","publishDate":"1936-06-10T00:00:00.000Z","related":[80,3]},{"id":2,"title":"Freelancer","publishDate":"2015-08-11T00:00:00.000Z","related":[45,89]},{"id":3,"title":"A Christmas Carol","publishDate":"1843-12-19T00:00:00.000Z","related":[20,33]},{"id":4,"title":"The Cat in the Hat","publishDate":"1957-03-12T00:00:00.000Z","related":[50,10]}]'
var res = JSON.parse(input);
console.log(res); /*
[[object Object] {
id: 1,
publishDate: "1936-06-10T00:00:00.000Z",
related: [80, 3],
title: "Gone with the Wind"
}, [object Object] {
id: 2,
publishDate: "2015-08-11T00:00:00.000Z",
related: [45, 89],
title: "Freelancer"
}, [object Object] {
id: 3,
publishDate: "1843-12-19T00:00:00.000Z",
related: [20, 33],
title: "A Christmas Carol"
}, [object Object] {
id: 4,
publishDate: "1957-03-12T00:00:00.000Z",
related: [50, 10],
title: "The Cat in the Hat"
}]
*/
JSON.parse(input, reviver), function can take a second object which is a reviver function:
for example, you can to parse this string:
var input = '[{"id":1,"title":"Gone with the Wind","publishDate":"1936-06-10T00:00:00.000Z","related":[80,3]},{"id":2,"title":"Freelancer","publishDate":"2015-08-11T00:00:00.000Z","related":[45,89]},{"id":3,"title":"A Christmas Carol","publishDate":"1843-12-19T00:00:00.000Z","related":[20,33]},{"id":4,"title":"The Cat in the Hat","publishDate":"1957-03-12T00:00:00.000Z","related":[50,10]}]'
to:
var expected = [
{id: 1, title: 'Gone with the Wind', publishDate: new Date('1936-06-10'), related: [80, 3]},
{id: 2, title: 'Freelancer', publishDate: new Date('2015-08-11'), related: [45, 89]},
{id: 3, title: 'A Christmas Carol', publishDate: new Date('1843-12-19'), related: [20, 33]},
{id: 4, title: 'The Cat in the Hat', publishDate: new Date('1957-03-12'), related: [50, 10]},
]
The difference is 'publishDate' is a Date object instead of string.
So what we can do is:
var input = '[{"id":1,"title":"Gone with the Wind","publishDate":"1936-06-10T00:00:00.000Z","related":[80,3]},{"id":2,"title":"Freelancer","publishDate":"2015-08-11T00:00:00.000Z","related":[45,89]},{"id":3,"title":"A Christmas Carol","publishDate":"1843-12-19T00:00:00.000Z","related":[20,33]},{"id":4,"title":"The Cat in the Hat","publishDate":"1957-03-12T00:00:00.000Z","related":[50,10]}]'; var expected = [
{id: 1, title: 'Gone with the Wind', publishDate: new Date('1936-06-10'), related: [80, 3]},
{id: 2, title: 'Freelancer', publishDate: new Date('2015-08-11'), related: [45, 89]},
{id: 3, title: 'A Christmas Carol', publishDate: new Date('1843-12-19'), related: [20, 33]},
{id: 4, title: 'The Cat in the Hat', publishDate: new Date('1957-03-12'), related: [50, 10]},
]; var result = JSON.parse(input, reviver); expect(result).toEqual(expected);
console.log("Test pass"); // function declarations
function reviver(key, value) {
if (key === '') { // handle root level object, the last key is ""
return value; // normal just need to return value, what you return here will be used as parsed value
} // handle the case you want to take care
if (key === 'publishDate') {
return new Date(value)
}
return value
}
[Javascript] JSON.parse API的更多相关文章
- JavaScript -- JSON.parse 函数 和 JSON.stringify 函数
JavaScript -- JSON.parse 函数 和 JSON.stringify 函数 1. JSON.parse 函数: 使用 JSON.parse 可将 JSON 字符串转换成对象. &l ...
- JavaScript JSON.parse()和JSON.stringify()
parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...
- javascript JSON.parse和eval的区别
SON.parse()用来将标准json字符串转换成js对象:eval()除了可以将json字符串(非标准的也可以,没有JSON.parse()要求严格)转换成js对象外还能用来动态执行js代码.例如 ...
- javascript JSON.parse and JSON.stringify
var jstu = '{"name": "xiaoqiang", "age": 18}'; console.log(jstu); var ...
- JSON.parse 函数
JSON.parse 函数 JavaScript JSON.parse 函数 (JavaScript) 将 JavaScript 对象表示法 (JSON) 字符串转换为对象. JSON.parse(t ...
- JavaScript的Eval与JSON.parse的区别
JavaScript的Eval与JSON.parse的区别 json的定义以及用法: JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格 ...
- 有关javascript中的JSON.parse和JSON.stringify的使用一二
有没有想过,当我们的大后台只是扮演一个数据库的角色,json在前后台的数据交换中扮演极其重要的角色时,作为依托node的前端开发,其实相当多的时间都是在处理数据,准确地说就是在处理逻辑和数据(这周实习 ...
- 将JSON格式数据转换为javascript对象 JSON.parse()
<html><body><h2>通过 JSON 字符串来创建对象</h3><p>First Name: <span id=" ...
- JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
随机推荐
- 如何清理多余的Windows桌面右键菜单
删除多余的发送到选项 Win7使用一段时间后,我们可能会装很多软件,这时候右键菜单可能会变得很长,特别是“发送到(Send to)”里面的选项,有时候我们卸载软件之后在右键发送到菜单里还会有残存的选项 ...
- PHP MySQLi
PHP MySQLi 简介 PHP MySQLi = PHP MySQL Improved! MySQLi 函数允许您访问 MySQL 数据库服务器. 注释:MySQLi 扩展被设计用于 MySQL ...
- php插入转义与查找转义
//转义用于查找 function deepslashes($data) { //判断data表现形式 if(empty($data)) { return $data; } return is_arr ...
- 使用2to3转换python代码
如果要把python2编译器下的代码转换升级到python3编译器,可以使用python自带的 2to3工具进行转化: windows下转化: doc 命令窗口: >> python C ...
- 开发ContentProvider的步骤
开发ContentProvider的步骤如下: 1.编写一个类,该类必须继承自ContentProvider. 2.实现ContentProvider类中的所有抽象方法:insert() delete ...
- ASP.NET执行cmd命令
批处理命令,是执行速度最快效益最高的命令.因为批处理命令,说白了,就是ms-dos环境下的命令,有很多的批处理命令,都是纯DOS下的命令. 然而,批处理命令尽管功能强大,却存在不足之处.批处理命令只能 ...
- Java导入证书失败Keystore was tampered with, or password was incorrect
keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect 在进行证书相关操作, ...
- 关于js闭包杂记
闭包:一个函数oneF里return了另一个函数innerF,然后在oneF外面运行了函数innerF,如果innerF里有用到在oneF里定义的变量,则此时依然可以引用到, 但是变量值不是定义函数i ...
- pip安装第三方库以及版本
这篇blog只是写给自己看看的. 今天突然遇到sqlalchemy映射到数据库时,一个字段类型是datetime(6),我这边死活访问不上,之前一直没有问题,最后查明原因,原来是第三方库的版本问题,真 ...
- 魔法方法:构造和析构 - 零基础入门学习Python041
魔法方法:构造和析构 让编程改变世界 Change the world by program 构造和析构 什么是魔法方法呢?我们来系统总结下: - 魔法方法总是被双下划线包围,例如__init__ - ...