[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,' ...
随机推荐
- sed删除空行和注释行
最近在看前辈们写的代码,他们把没有用的代码是注释掉而不是删掉.没用的代码和注释很乱,看着心烦,就把注释删掉来解读,顿时爽快多了. 不多说了,直接举例子 比如一个文本文件 data 里的内弄为 cat ...
- 02-大文件Copy(FileStream文件流类)
static void Main(string[] args) { string source = @"e:\1.exe";//要移动文件的路径 大文件 string target ...
- hdu 1042
貌似之前也写过这个题目的解题报告...老了,记性不好 从贴一遍吧! 代码理解很容易 AC代码: #include <iostream> #include <stdio.h> # ...
- firefox 不能显示 glyphicons 字体
折腾了很久才发现是firefox 不能跨域下载相应的字体文件,将bootstrap相应的css文件和字体文件copy到调用的项目里,问题才得以解决.
- mybatis的简单使用
使用mybatis数据库时,需要添加一下jar包: asm-3.3.1.jarcglib-2.2.2.jarjavassist-3.17.1-GA.jarlog4j-1.2.17.jarmybatis ...
- Spring框架快速入门之简介
Spring是java平台上的一个开源应用框架.它的第一个版本是由Rod Johnson写出来的.Rod在他的Expert One-On- One Java EE Design and Develop ...
- redis cluster 集群搭建步骤和注意事项
1.安装Ubuntu ,修改root的密码. sudo passwd (apt-get update 更新系统) 2.安装 Gcc 和G++ sudo apt-get install build- ...
- Objective-C 引用计数:不讲用法,只说原理
本文所使用的源码为 objc4-647 和 CF-1153.18 实际上这是我本周实习周报的一部分,写的比较仓促,如有差错还请多多指正. 不讲用法,只说原理. 引用计数如何存储 有些对象如果支持使用 ...
- 关于PagedDataSource,非常好用的一个分页属性!
Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...
- 基于live555的一个简单RTSP服务器
1,编译live555源码目录下的 BasicUsageEnvironment.groupsock.liveMedia.UsageEnvironment四个工程生成相应的库文件: 目录结构如下: 2, ...