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. Js实现简单的联动,无数据库版本

    <html> <head> <title></title> <script language="javascript" typ ...

  2. js获取当前事件键盘按钮

    用户名:<input type="text" name="username" id="username"><br /> ...

  3. JAVA打印类(带预览)

    package tool; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; ...

  4. Domino 8.5 WebService开发一例

    原文地址:Domino 8.5 WebService开发一例作者:bj木棉 需求是要调用一个Domino上的WebService/JAVA来实现与人事管理系统里的人员同步,就是在人事管理系统中增加用户 ...

  5. hdu4430 枚举+二分

    Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. vs2008下使用libcurl

    网上找了半天,总算找到一个比较好用的C++ 网络库,老实说,完全用Socket操作网络对于需要开发网络应用程序的人员来说还是很蛋疼很繁琐的.好在有这么一个给力的库.这个库的介绍可以自己百度一下,就我所 ...

  7. javascript之闭包深入理解(一)

    曾经在开始学习javascript的时候,很是不理解闭包的概念.今天想对它详细的剖析. 在说清楚闭包之前,必须先清楚作用域链. 作用域链 我们知道,执行环境是js中最为重要的一个概念.执行环境定义了变 ...

  8. Div在BOdy中居中

    <h1 style="position: absolute; width: 500px; height:200px; left:%; top:%; margin-left:-250px ...

  9. Js配合CSS实现的图片居中

    CSS图上居中很好实现,但万恶的浏览器之间各不相让,搞的不兼容,还好我们有让其兼容的办法,那就是结合JS来实现,这样各个浏览器都听话多了.本例就是CSS结合JavaScript实现的图片垂直.水平方向 ...

  10. OpenStack JEOS 镜像

    JEOS:Just Enough Operating System 维基百科地址:http://en.wikipedia.org/wiki/Just_enough_operating_system O ...