如何jsonify包含自定义jsonification对象的数组

时间:2022-04-09 19:43:20

So I have a CustomObject and an array containing a number of them.

所以我有一个CustomObject和一个包含许多它们的数组。

Right now, when I say res.json(myArray), I get a standard jsonification of my CustomObject, i.e. field1:value, field2:value, ....

现在,当我说res.json(myArray)时,我得到了我的CustomObject的标准jsonification,即field1:value,field2:value,....

What I can not figure out is how to make a custom jsonification of my CustomObject, such as I only want to return a subset of the parameters.

我无法弄清楚的是如何对我的CustomObject进行自定义jsonification,例如我只想返回参数的子集。

What functions do I have to customize? In CustomObject only?

我有哪些功能可以自定义?仅在CustomObject中?

1 个解决方案

#1


3  

What functions do I have to customize? In CustomObject only?

我有哪些功能可以自定义?仅在CustomObject中?

Give your objects a toJSON method that returns an object with only the expected properties and it will be recognized by JSON.stringify.

为对象提供一个toJSON方法,该方法返回仅具有预期属性的对象,并且JSON.stringify将识别该对象。

#1


3  

What functions do I have to customize? In CustomObject only?

我有哪些功能可以自定义?仅在CustomObject中?

Give your objects a toJSON method that returns an object with only the expected properties and it will be recognized by JSON.stringify.

为对象提供一个toJSON方法,该方法返回仅具有预期属性的对象,并且JSON.stringify将识别该对象。