js对象转json对象(post请求传递json参数)
在javascript中,json.stringify()方法用于将 javascript 对象或值转换为 json 字符串。如果对象有tojson方法,json.stringify 就会调用对象的tojson方法,以tojson方法返回的值为序列化值 。
举个例子,以下脚本等效于 json.stringify({ answer: 42 }).
const json = json.stringify({
answer: { tojson: () => 42 }
});
console.log(json); // {"answer":42}
与 es6 类结合
tojson非常有利于es6类对象正常序列化。举个例子,你通过error类型扩展一个httperror类。
class httperror extends error {
constructor(message, status) {
super(message);
this.status = status;
}
}
javascript不会很好的序列化错误信息。默认情况下,以下脚本只会输出:{“status”:404},没有错误信息及错误堆栈跟踪。
class httperror extends error {
constructor(message, status) {
super(message);
this.status = status;
}
}
const e = new httperror('fail', 404);
console.log(json.stringify(e)); // {"status":404}
但如果在httperror类型中添加tojson方法,效果会好很多。
class httperror extends error {
constructor(message, status) {
super(message);
this.status = status;
}
tojson() {
return { message: this.message, status: this.status };
}
}
const e = new httperror('fail', 404);
console.log(json.stringify(e)); // {"message":"fail","status":404}
你还可以通过tojson方法添加更多调试信息,假如你的node_env处于开发环境,你可以为错误添加堆栈信息,方便调试。
class httperror extends error {
constructor(message, status) {
super(message);
this.status = status;
}
tojson() {
const ret = { message: this.message, status: this.status };
if (process.env.node_env === 'development') {
ret.stack = this.stack;
}
return ret;
}
}
const e = new httperror('fail', 404);
// {"message":"fail","status":404,"stack":"error: fail\n at ...
console.log(json.stringify(e));
tojson最好的地方是可以帮你处理嵌套信息。通过tojson你仍然可以正确地序列化数组中深层嵌套的httperror实例和httperror实例。
class httperror extends error {
constructor(message, status) {
super(message);
this.status = status;
}
tojson() {
return { message: this.message, status: this.status };
}
}
const e = new httperror('fail', 404);
// {"nested":{"message":"fail","status":404},"arr":[{"message":"fail","status":404}]}
console.log(json.stringify({
nested: e,
arr: [e]
}));
很多类库都是通过tojson来json.stringify自定义化。如 express 的res.json()方法、axios post requests序列化对象等。
tojson() 实际应用
moment.js类库为对象自定义了tojson方法:
function tojson () {
// json.stringify(new date(nan)) === 'null'
return this.isvalid() ? this.toisostring() : 'null';
}
你可以直接调用tojson序列化日期对象:
const moment = require('moment');
console.log(moment('2019-06-01').tojson.tostring());
node.js buffers 对象也自定tojson方法:
const buf = buffer.from('abc');
console.log(buf.tojson.tostring());
// prints:
function tojson() {
if (this.length > 0) {
const data = new array(this.length);
for (var i = 0; i < this.length; ++i)
data[i] = this[i];
return { type: 'buffer', data };
} else {
return { type: 'buffer', data: [] };
}
}
mongoose文档对象也具有tojson()函数,以确保mongoose文档对象的内部状态不会出现在json.stringify()输出中。
小结
tojson()函数是javascript构建类时重要的工具。通过这种方式,您可以控制javascript如何将你的类实例序列化为json字符串。 tojson()函数可以帮助您解决许多问题,例如确保日期对象得到正确的格式或node.js缓冲对象正常序列化。下次构造es6类型时,一定要记得尝试哦。
上一篇: 前端快速开发框架有哪些(网页前端开发)
下一篇: JSP的相对路径如何计算深入研究
推荐阅读
-
json转js对象的方法(0基础小白深入掌握Json)
-
js对象转json字符串(js中将json对象转换成字符串)
-
js对象转json字符串(js中将json对象转换成字符串)
-
js对象转json对象(post请求传递json参数)
-
json转js对象的方法(0基础小白深入掌握Json)
-
前端js把json字符串转json对象(java对象转json对象命令)
-
js中json字符串转json对象的方法(提取json格式的数据)
-
前端js把json字符串转json对象(java对象转json对象命令)
-
JS获取url参数,JS发送json格式的POST请求方法
-
js实现的xml对象转json功能示例