[Javascript] The JSON.stringify 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 serialize a JavaScript object or array into aJSON string. This API allows you to customize the serialization very specifically as well.
console.clear() testValue()
testSpaces()
testFunctionReplacer()
testArrayReplacer()
testSymbolKeyedValues()
testNonEnumerableValue()
testToJSON() console.log('Tests passed') // function declarations
function testValue() {
var input = {
name: 'Kent C. Dodds',
username: 'kentcdodds',
}
var expected = '{"name":"Kent C. Dodds","username":"kentcdodds"}'
var result = JSON.stringify(input) expect(result).toEqual(expected)
} function testSpaces() {
var input = {
name: {
first: 'Kent',
middle: 'Christopher',
last: 'Dodds'
},
username: 'kentcdodds',
}
var expected = `{
"name": {
"first": "Kent",
"middle": "Christopher",
"last": "Dodds"
},
"username": "kentcdodds"
}`
var result = JSON.stringify(input, null, 2) expect(result).toEqual(expected)
} function testFunctionReplacer() {
var input = {
title: 'Gone with the Wind',
publishDate: new Date('1936-06-10'),
movieReleaseDate: new Date('1940-01-17'),
} var expected = '{"title":"Gone with the Wind","publishDate":"1936-06-10","movieReleaseDate":"1940-01-17"}' var result = JSON.stringify(input, replacer) expect(result).toEqual(expected) function replacer(key, value) {
// `this` is bound to the object in which the value was found
if (this[key] instanceof Date) {
return value.substring(0, 10)
}
return value
}
} function testArrayReplacer() {
var input = [
{id: 3, title: 'Inside Out', rating: 98, genres: ['Animation', 'Kids & Family']},
{id: 6, title: 'The Hunger Games', rating: 84, genres: [' Drama', 'Mystery & Suspense', 'Science Fiction & Fantasy']},
{id: 13, title: 'Catch Me If You Can', rating: 96, genres: ['Drama', 'Action & Adventure']},
]
var expected = '[{"id":3,"title":"Inside Out"},{"id":6,"title":"The Hunger Games"},{"id":13,"title":"Catch Me If You Can"}]'
var result = JSON.stringify(input, ['id', 'title']) expect(result).toEqual(expected)
} function testSymbolKeyedValues() {
var input = {foo: 'foo'}
var barSymbol = Symbol('bar')
input[barSymbol] = 'bar' var expected = '{"foo":"foo"}'
var result = JSON.stringify(input) expect(result).toEqual(expected)
} function testNonEnumerableValue() {
var input = Object.create(null, {
theAnswer: {
value: 42,
enumerable: true
},
theQuestion: {
value: 'Who knows...',
enumerable: false
}
}) var expected = '{"theAnswer":42}'
var result = JSON.stringify(input) expect(result).toEqual(expected)
} function testToJSON() {
var input = {
name: {
first: 'Dave',
middle: 'James',
last: 'Smith',
toJSON: function(key) {
return {awesomeName: `The Awesome ${this.first} ${this.middle} ${this.last}`}
console.log(key, this)
},
},
username: 'djsmith42',
} var expected = '{"name":{"awesomeName":"The Awesome Dave James Smith"},"username":"djsmith42"}'
var result = JSON.stringify(input) expect(result).toEqual(expected)
}
[Javascript] The JSON.stringify API的更多相关文章
- (八)JavaScript之[JSON]与[void]
14].JSONJSON 格式在语法上与创建 JavaScript 对象代码是相同的. 方法:JSON.parse(); //将JSON字符串转换为JavaScript对象JSON.stringify ...
- JSON.stringify 函数 (JavaScript)
在bsrck项目中,使用jQuery.Form.js的ajaxSubmit时,遇到有文件上传的form提交,在firefox和chrome浏览器中测试,报Bad Request的错误,经查代码后发现是 ...
- 有关javascript中的JSON.parse和JSON.stringify的使用一二
有没有想过,当我们的大后台只是扮演一个数据库的角色,json在前后台的数据交换中扮演极其重要的角色时,作为依托node的前端开发,其实相当多的时间都是在处理数据,准确地说就是在处理逻辑和数据(这周实习 ...
- JavaScript -- JSON.parse 函数 和 JSON.stringify 函数
JavaScript -- JSON.parse 函数 和 JSON.stringify 函数 1. JSON.parse 函数: 使用 JSON.parse 可将 JSON 字符串转换成对象. &l ...
- JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
- JSON 字符串转换为JavaScript 对象.JSON.parse()和JSON.stringify()
使用 JavaScript 内置函数 JSON.parse() 将字符串转换为 JavaScript 对象: var text = '{ "sites" : [' + '{ &qu ...
- ZH奶酪:JavaScript中的JSON.stringify() and JSON.parse()
JSON.stringify() JSON.stringify()可以将任意的JavaScript值序列化成JSON字符串. 语法 JSON.stringify(value[, replacer [, ...
- javascript 中的JSON.stringify - 将对象和数组转换为json格式(来源于网络)
JSON.stringify 函数 (JavaScript) 将 JavaScript 值转换为 JavaScript 对象表示法 (Json) 字符串. JSON.stringi ...
- javascript 数组和对象的浅复制和深度复制 assign/slice/concat/JSON.parse(JSON.stringify())
javascript 数组和对象的浅度复制和深度复制在平常我们用 ‘=’来用一个变量引用一个数组或对象,这里是‘引用’而不是复制下面我们看一个例子引用和复制是什么概念 var arr=[1,2,3,' ...
随机推荐
- android ViewFlipper的使用
有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果.该类有如下几个和动画 ...
- [编辑中] 免费的Internet流量发生器 | Free Internet Traffic Generators
流量发生器 (Traffic Generator) 是用来检测网络性能,进行网络相关研究的一个很重要的工具.大家可能用过Iperf或者IxChariot,前者是类UNIX环境下的一个免费.开源的网络性 ...
- Android(java)学习笔记260:JNI之native方法头文件的生成
1. JDK1.6 ,进入到工程的bin目录下classes目录下: 使用命令: javah packageName.ClassName 会在当前目录下生成头文件,从头文件找到jni协议方法 下面举 ...
- 多线程下载 HttpURLConnection
Activity /**实际开发涉及文件上传.下载都不会自己写这些代码,一般会使用第三方库(如xUtils)或Android提供的DownloadManager下载*/ public class Ht ...
- JAVA-FileInputStream之read方法
今天一个友询问FileInputStrem方法的read()和read(byte b) 方法为什么都用-1来判断读文件结束的问题,在此和大家一起学习下. 关于FileInputStream 它用于读取 ...
- (转)ASP.NET 伪静态
1.伪静态:http://www.cnblogs.com/Default.html 目的就是为了赢得更多的收入,至于真否,待SEOer 解答,正如文字所说,伪静态就是假的静态. 2.准备工作:下载Ur ...
- java开发中的23中设计模式
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- JSTL核心标签库
1.set:给web域设置值的 <c:set var="lang" value="Java" scope="page">< ...
- Spring MVC异常处理
Spring Mvc 中异常处理,一般有两种解决办法: 一.利用org.springframework.web.servlet.handler.SimpleMappingExceptionResolv ...
- VS2013配合EgretVS开发简单塔防游戏
VS2013配合EgretVS开发简单塔防游戏(1) - 环境配置 VS2013配合EgretVS开发简单塔防游戏(2) – 原型设计 VS2013配合EgretVS开发简单塔防游戏(3) – 精灵动 ...