[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,' ...
随机推荐
- Java调用天气Webservice的小应用
废话不多说,直接贴代码: CityReq.java package com.weather; import javax.xml.bind.annotation.XmlElement; import j ...
- Python代码分析工具之dis模块
转自:http://hi.baidu.com/tinyweb/item/923d012e8146d00872863ec0 ,格式调整过. 代码分析不是一个新的话题,代码分析重要性的判断比较主观,不同 ...
- 1:环境安装与介绍:canopy
<利用python进行数据分析>这本书推荐用的的环境为EPDFree版本,但实际现在大概已经抛弃它改用Canopy了,下面将介绍Canopy相关: 一:下载:https://store.e ...
- ASP.NET MVC3.0或4.0设置二级域名的方法
之前我就想做二级域名指向同一个IP同一个程序无非是在路由匹配规则上做文章也就是对Url的重写的一种思路.我用了半天时间上网查阅了相关资料并做了Demo测试是完全 以的,在这分享给大家... 假如网站主 ...
- 学习Android MediaPlayer
Android Media Playback 原文 The Android multimedia framework includes support for playing variety of c ...
- winform 获取当前项目所在的路径
代码: string curAppPath = System.IO.Directory.GetParent(System.Environment.CurrentDirectory).Parent.Fu ...
- [c#]asp.net开发微信公众平台(3)微信消息封装及反射赋值
上一篇已经搭建好整体框架,实现了入口的验证, 验证通过后就交给LookMsgType方法处理,LookMsgType方法主要是对微信发来的不同的消息进行分解,不同的类型交给业务逻辑层不同的方法处理 ...
- C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询
1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...
- java下载文件
/** * * @param file * @param response */ private void downLoad(File file, HttpServletResponse respon ...
- static在类中的定义,和enum的用法
class A { // static int a = 1;//错误,静态变量在类外定义 static int a; static const int b = 1;//如果是静态成员常量,则可以在类内 ...