let foo = { a: 2, b: function() {} };
JSON.stringify(foo);
// "{ "a": 2 }"
var bar = {
a: {
c: foo
}
};
var foo = {
b: bar
};
let fooStringified = JSON.stringify(foo);
console.log(fooStringified); // {"b":{"a":{}}}
let foo = {b : foo};
ReferenceError: foo is not defined at repl:1:14
let foo = { b: undefined };
JSON.stringify(foo);
// {}
// Symbols
foo.b = Symbol();
JSON.stringify(foo);
// {}
let foo = [Symbol(), undefined, function() {}, 'works']
JSON.stringify(foo);
// "[null,null,null,'works']"
function Person (first, last) {
this.firstName = first;
this.last = last;
}
Person.prototype.process = function () {
return this.firstName + ' ' +
this.lastName;
};
let ade = new Person('Ade', 'P');
JSON.stringify(ade);
// "{"firstName":"Ade","last":"P"}"
Person.prototype.toJSON = function () {
return { fullName: this.process(); };
};
let ade = new Person('Ade', 'P');
JSON.stringify(ade);
// "{"fullName":"Ade P"}"
// jQuery
$.post('endpoint', ade);
// Angular 2
this.httpService.post('endpoint', ade)
JSON.stringify(value, replacer?, space?)
let foo = {
a : 1,
b : "string",
c : false
};
JSON.stringify(foo, ['a', 'b']);
//"{"a":1,"b":"string"}"
let bar = {
a : 1,
b : { c : 2 }
};
JSON.stringify(bar, ['a', 'b']);
//"{"a":1,"b":{}}"
JSON.stringify(bar, ['a', 'b', 'c']);
//"{"a":1,"b":{"c":2}}"
let baz = {
a : 1,
b : { c : 2 }
};
// 返回大于1的值
let replacer = function (key, value) {
if(typeof === 'number') {
return value > 1 ? value: undefined;
}
return value;
};
JSON.stringify(baz, replacer);
// "{"b":{"c":2}}"
let obj = {
a : 1,
b : { c : 2 }
};
let tracer = function (key, value){
console.log('Key: ', key);
console.log('Value: ', value);
return value;
};
JSON.stringify(obj, tracer);
// Key:
// Value: Object {a: 1, b: Object}
// Key: a
// Value: 1
// Key: b
// Value: Object {c: 2}
// Key: c
// Value: 2
let space = {
a : 1,
b : { c : 2 }
};
// 使用制表符
JSON.stringify(space, undefined, '\t');
// "{
// "a": 1,
// "b": {
// "c": 2
// }
// }"
JSON.stringify(space, undefined, '');
// {"a":1,"b":{"c":2}}
// 自定义分隔符
JSON.stringify(space, undefined, 'a');
// "{
// a"a": 1,
// a"b": {
// aa"c": 2
// a}
// }"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有