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的更多相关文章

  1. JavaScript -- JSON.parse 函数 和 JSON.stringify 函数

    JavaScript -- JSON.parse 函数 和 JSON.stringify 函数 1. JSON.parse 函数: 使用 JSON.parse 可将 JSON 字符串转换成对象. &l ...

  2. JavaScript JSON.parse()和JSON.stringify()

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...

  3. javascript JSON.parse和eval的区别

    SON.parse()用来将标准json字符串转换成js对象:eval()除了可以将json字符串(非标准的也可以,没有JSON.parse()要求严格)转换成js对象外还能用来动态执行js代码.例如 ...

  4. javascript JSON.parse and JSON.stringify

    var jstu = '{"name": "xiaoqiang", "age": 18}'; console.log(jstu); var ...

  5. JSON.parse 函数

    JSON.parse 函数 JavaScript JSON.parse 函数 (JavaScript) 将 JavaScript 对象表示法 (JSON) 字符串转换为对象. JSON.parse(t ...

  6. JavaScript的Eval与JSON.parse的区别

    JavaScript的Eval与JSON.parse的区别 json的定义以及用法: JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格 ...

  7. 有关javascript中的JSON.parse和JSON.stringify的使用一二

    有没有想过,当我们的大后台只是扮演一个数据库的角色,json在前后台的数据交换中扮演极其重要的角色时,作为依托node的前端开发,其实相当多的时间都是在处理数据,准确地说就是在处理逻辑和数据(这周实习 ...

  8. 将JSON格式数据转换为javascript对象 JSON.parse()

    <html><body><h2>通过 JSON 字符串来创建对象</h3><p>First Name: <span id=" ...

  9. JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象

    JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象

随机推荐

  1. 关于ISAPI和CGI限制,这个要设为允许

    否则程序就报这个错误,注意,设置允许时不是在添加的网站上设置,而是在根iis,选择后右侧出现关于ISAPI和CGI限制,进去后选择相应版本,设置为允许就可以了

  2. 浅谈PHP代码设计结构

    浅谈PHP代码设计结构 您的评价:       还行  收藏该经验       coding多年,各种代码日夜相伴,如何跟代码友好的相处,不光成为职业生涯的一种回应,也是编写者功力的直接显露. 如何看 ...

  3. WinAPI——模拟正弦波

    /*************************** * * 程序名称 : 模拟正弦波 * 作 者 : doodle777 * 版 本 : 1.1 * 日 期 : 2012-10-19 * 说 明 ...

  4. Shell中的${},##和%%的使用

    假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值: ${file#*/}:删掉第一个/ 及其左边的字符串:dir1/di ...

  5. Jsp通过JDBC连接到SQL Server2008数据库遇到的几个问题

    今天刚刚学jdbc ,之前通过main连到过Sql server 2008现在通过jsp连接sql server 2008,遇到了一些问题,跟大家分享一下,避免少走弯路 一.ClassNotFound ...

  6. stax解析XML-----Demo

    Demo public void staxDemo() throws Exception { XMLStreamReader xml= XMLInputFactory.newInstance().cr ...

  7. [转]C++宏定义详解

    一.#define的基本用法     #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率,但学生在学习时往往不能 理解该命令的本质 ...

  8. MySql中的事务嵌套

    1.Mysql中的事务必须是InnoDB.Berkeley DB引擎,myisam不支持. 2.Mysql是不支持嵌套事务的,开启了一个事务的情况下,再开启一个事务,会隐式的提交上一个事务. 3.My ...

  9. linux定时任务crond那些事!

    1.定时任务crond介绍 1.1 crond是什么 crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件. 特殊需求:(秒级别)crond服务就无法搞定了,一般工作中写脚本守护 ...

  10. php开发通用采集程序

    php采集程序构建基本步骤: 采集程序是什么?获取远程数据(文字.图片.图片)并快速保存到本地或指定地址. 如天气预报(小偷程序): 远程获取-->替换内容-->展示给用户 如实时更新的新 ...