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. Eclipse利用代理快速安装插件

    在eclipse启动时增加以下参数: eclipse.exe -vmargs -DproxySet=true -DproxyHost=aProxyAddress -DproxyPort=aProxyP ...

  2. java反射新的应用

    利用java反射动态修改运行中对象的私有final变量,不管有没有get方法获取这个私有final变量. spring aop 本质是cglib,动态代理 可以做很多事情 query.addCrite ...

  3. (转)ASP.NET里面简单的记住用户名和密码

    using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using S ...

  4. java中保留几位小数

    public class NumUtils { /** * 保留两位小数 * * @param d * @return */ public static String get2Wei(double d ...

  5. oracle单行函数之数字函数

    round--四舍五入函数 trunc--截断函数 mod--求余函数 round和trunc除了可以操作数字外还可以操作日期. Demo SQL),),),),) from dual ; ) ) ) ...

  6. 武汉科技大学ACM:1008: 明明的随机数

    Problem Description 明明想在学校中请一些同学一起做一项问卷 调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个, ...

  7. String类扩展

    String s1=new String("fsdfsd"); String s2=new String("fsdfsd"); String a1=" ...

  8. 我是一个云Hosts文件,用来干啥你懂的

    Smarthosts是一个托管在谷歌代码上的项目,您可以轻松利用本项目使用到一份稳定的Hosts文件. 这份Hosts文件可以帮助您顺利打开一些网站,提高某些国外服务的打开或下载速度. http:// ...

  9. 利用shell脚本函数实现网络连通性的检测

    通常情况下,linux系统网络连通性的检测有两种方法: 1.通过“ping指令 + 目标IP” 或者 “ping指令 + 目标IP域名” 注意针对IPv6地址的IP网络检测需要试用ping6,同时pi ...

  10. 汉诺塔 Hanoi Tower

    电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...