简体   繁体   English

在json序列化期间排除字段

[英]Excluding fields during json serialization

I'm trying to use FlexJSON to serialize a java object. 我正在尝试使用FlexJSON序列化Java对象。 The object has some top level fields and a collection called results. 该对象具有一些顶级字段和一个名为results的集合。 From within the collection I only want a few properties but all of them are being serialized. 从集合中,我只需要几个属性,但是所有属性都已被序列化。 Here is the code I'm trying. 这是我正在尝试的代码。

jsons = new JSONSerializer().include("results.ourID","results.name","results.fmtDistance","results.shows.showName","results.knownForNoHTML").exclude("results");

I've also tried results.* in the exclude. 我也尝试过results。*在排除中。 No luck - all of the results fields are serialized. 运气不好-所有结果字段都已序列化。

Instead of .exclude("results"); 代替.exclude("results"); you should use .exclude("*"); 您应该使用.exclude("*"); . This will exclude all other parameters except those you've specified. 这将排除您指定的所有其他参数。

So in this case your line should be: 因此,在这种情况下,您的行应为:

jsons = new JSONSerializer().include("results.ourID","results.name","results.fmtDistance","results.shows.showName","results.knownForNoHTML").exclude("*");

From http://flexjson.sourceforge.net/ http://flexjson.sourceforge.net/

Using dot notation with exludes has a subtle difference in it's use when compared with includes. 与包含相比,将点符号与排除一起使用时,其用法有细微的差别。 If you exclude a nested field it implies that the rest of the parent object is included. 如果排除嵌套字段,则表示包括了父对象的其余部分。 So if I say exclude("head.rightEye.retinalScan"). 因此,如果我说排除(“ head.rightEye.retinalScan”)。 The retinalScan member of rightEye field will be excluded, but both rightEye and head field will be included. rightEye字段的retinalScan成员将被排除,但rightEye和head字段将被包括在内。 The reason is in order to exclude retinalScan field you have to include the rightEye member. 原因是为了排除retinalScan字段,您必须包括rightEye成员。 If you didn't then it doesn't change anything because retinalScan wasn't going to be included in the first place. 如果您不这样做,那么它不会有任何改变,因为不会首先包含retinalScan。 Said another way it's only the last field that is excluded all other parent fields are included. 换句话说,只有最后一个字段被排除,所有其他父字段都被包括在内。

That suggests you should only use an Exclude for the collection, indicating what you don't want and then it will automatically include what you do want. 这表明您只应对集合使用排除项,指示您不需要的内容,然后它将自动包含您想要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM