在对数组对象进行json字符串化时排除对象属性 [英] Excluding object properties while json-stringifying array object

查看:191
本文介绍了在对数组对象进行json字符串化时排除对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个类似这样的数组对象

hey I have a array object something like this

[{
    public: "public",
    private: "private",
    [{
        properties: {...
        },
        instance: {.....
        }
    }, {...
    }, {...
    }]
}, {...
}, {....
}]

在此,最外面的数组包含A类的对象,该对象具有一些公共道具,一些私密的对象,并且还包含一个包含B类的对象的数组,该对象还包含一些公共字段和私有字段.

Here the outer most array contains object of class A, which has a some public props, some private porps and it also contains an array which contains object of class B, which also contains some public and private fields.

所以基本上这是我的等级制度

so basically this is my hierarchy

array = [A1,A2,A3,A4,....]//object of A

var A = function(){
  var Const = function(){
    this.public_prop;
    this.private_prop;
    this.list = [B1,B2,B3,B4]// objects of B
  }
 //.........
 return Const;
}();

var B = function(){

  var Const = function(){
   this.public_prop;
   this.private_prop;
  }
 //.........
 return Const;
}();

现在在进行字符串化(序列化)时,我只想在序列化字符串中包含public prop和数组.

Now while stringifying(serialzing) it I want to only include public prop and the arrays in the serialized string.

例如,对于上述JSON表示形式,我想要这样的东西

for example for the above JSON representation i want something like this

[{
        public: "public",
        [{
            properties: {...
            }
        }, {...
        }, {...
        }]
    }, {...
    }, {....
    }]

现在我可以在每个类中创建一个函数 getState(),该函数将仅返回需要进行字符串化的字段,但是我似乎找不到一种方法来实现JSON.stringify的本机实现.在序列化之前调用该方法.有什么方法可以做到这一点?

now i can create a function getState() in each class which will only return fields which needs to be stringified, but i cannot seem to find a way to make the native implementation of JSON.stringify call the method before serializing it. Is there some way of accomplishing this?

我引用了在输出中隐藏某些值从JSON.stringify()中删除,但是仅说明了如何在单个层次结构中排除简单的数字或字符串属性,但是如何在多个层次结构中排除属性?

I refered Hide certain values in output from JSON.stringify(), but it only explains how to exclude simple numeric or string prop in single hierarchy, but how to exclude props in multiple hierarchy?

注意:我所有的课程都遵循模块模式

推荐答案

假设您正在按照问题标签的建议编写JavaScript(尽管示例代码看起来几乎是c#!):您需要覆盖"toJSON"您要序列化的对象的方法,而不是"stringify"或"getState".

Assuming that you're writing JavaScript as your question tags suggest (although your example code looks like it's nearly c#!): you need to override the "toJSON" method of the object you're serializing, not "stringify" nor "getState".

因此,如果对象"Message"具有公共和私有"属性,则需要定义一个"toJSON"方法,该方法仅返回公共属性,如下所示:

Therefore if you have an object "Message" that has public and "private" properties, you need to define a "toJSON" method that only returns the public property, as shown below:

var Message = function() {
    this.myPrivateProperty = "Secret message";
    this.myPublicProperty = "Message for the public";

    this.toJSON = function() {
        return {
            "public": this.myPublicProperty
        };
    };
}


alert(JSON.stringify(new Message()));    // {"public":"Message for the public"}

这篇关于在对数组对象进行json字符串化时排除对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆