当前位置: 首页 > news >正文

建设工程公司采购的网站/打开百度首页

建设工程公司采购的网站,打开百度首页,国家认可的赚钱游戏无广告,品牌营销策划方案怎么做才好SerializeFilter是通过编程扩展的方式定制序列化。Fastjson 支持6种 SerializeFilter,用于不同场景的定制序列化。 PropertyPreFilter:根据 PropertyName 判断是否序列化PropertyFilter:根据 PropertyName 和 PropertyValue 来判断是否序列化…

SerializeFilter是通过编程扩展的方式定制序列化。Fastjson 支持6种 SerializeFilter,用于不同场景的定制序列化。

  • PropertyPreFilter:根据 PropertyName 判断是否序列化

  • PropertyFilter:根据 PropertyName 和 PropertyValue 来判断是否序列化

  • NameFilter:修改 Key,如果需要修改 Key,process 返回值则可

  • ValueFilter:修改 Value

  • BeforeFilter:序列化时在最前添加内容

  • AfterFilter:序列化时在最后添加内容

1. 需求

JSON 数据格式如下,需要过滤掉其中 "book" 的 "price" 属性。

JSON数据格式:

{"store": {"book": [{"category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{"category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99}],"bicycle": {"color": "red","price": 19.95}},"expensive": 10
}

2. SimplePropertyPreFilter 过滤器

该过滤器由 Fastjson 提供,代码实现: 

String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("price");
JSONObject jsonObject = JSON.parseObject(json);
String str = JSON.toJSONString(jsonObject, filter);
System.out.println(str);

运行结果:

{"store": {"bicycle": {"color": "red"},"book": [{"author": "Nigel Rees","category": "reference","title": "Sayings of the Century"},{"author": "Evelyn Waugh","category": "fiction","title": "Sword of Honour"}]},"expensive": 10
}

查看 JSON 数据的过滤结果,发现 "bicycle" 中的 "price" 属性也被过滤掉了,不符合需求。

3. LevelPropertyPreFilter 过滤器

该自定义过滤器实现 PropertyPreFilter 接口,实现根据层级过滤 JSON 数据中的属性。
扩展类:

/*** 层级属性删除* * @author yinjianwei* @date 2017年8月24日 下午3:55:19**/
public class LevelPropertyPreFilter implements PropertyPreFilter {private final Class<?> clazz;private final Set<String> includes = new HashSet<String>();private final Set<String> excludes = new HashSet<String>();private int maxLevel = 0;public LevelPropertyPreFilter(String... properties) {this(null, properties);}public LevelPropertyPreFilter(Class<?> clazz, String... properties) {super();this.clazz = clazz;for (String item : properties) {if (item != null) {this.includes.add(item);}}}public LevelPropertyPreFilter addExcludes(String... filters) {for (int i = 0; i < filters.length; i++) {this.getExcludes().add(filters[i]);}return this;}public LevelPropertyPreFilter addIncludes(String... filters) {for (int i = 0; i < filters.length; i++) {this.getIncludes().add(filters[i]);}return this;}public boolean apply(JSONSerializer serializer, Object source, String name) {if (source == null) {return true;}if (clazz != null && !clazz.isInstance(source)) {return true;}// 过滤带层级属性(store.book.price)SerialContext serialContext = serializer.getContext();String levelName = serialContext.toString();levelName = levelName + "." + name;levelName = levelName.replace("$.", "");levelName = levelName.replaceAll("\\[\\d+\\]", "");if (this.excludes.contains(levelName)) {return false;}if (maxLevel > 0) {int level = 0;SerialContext context = serializer.getContext();while (context != null) {level++;if (level > maxLevel) {return false;}context = context.parent;}}if (includes.size() == 0 || includes.contains(name)) {return true;}return false;}public int getMaxLevel() {return maxLevel;}public void setMaxLevel(int maxLevel) {this.maxLevel = maxLevel;}public Class<?> getClazz() {return clazz;}public Set<String> getIncludes() {return includes;}public Set<String> getExcludes() {return excludes;}
}

代码实现:

public static void main(String[] args) {String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";JSONObject jsonObj = JSON.parseObject(json);LevelPropertyPreFilter propertyPreFilter = new LevelPropertyPreFilter();propertyPreFilter.addExcludes("store.book.price");String json2 = JSON.toJSONString(jsonObj, propertyPreFilter);System.out.println(json2);
}

运行结果: 

{"store": {"bicycle": {"color": "red","price": 19.95},"book": [{"author": "Nigel Rees","category": "reference","title": "Sayings of the Century"},{"author": "Evelyn Waugh","category": "fiction","title": "Sword of Honour"}]},"expensive": 10
}

查看 JSON 数据的过滤结果,实现了上面的需求。

参考:http://www.cnblogs.com/dirgo/...

http://www.jmfq.cn/news/4766077.html

相关文章:

  • 管理系统 网站模板/代运营套餐价格表
  • 网站开发南昌/永久免费制作网页
  • 网站独立ip/合肥关键词排名技巧
  • 做网站python好还是java/国际新闻最新
  • 手机能用的网站/如何制作自己的链接
  • 安徽合肥做网站/百度搜索排名购买
  • 用电脑做兼职的网站比较好/全网引擎搜索
  • 做一个网站的成本/google官网
  • 库尔勒网站建设价格/新闻发布会
  • 东莞怎么制作网站/查排名官网
  • 东营做营销型网站建设/在线培训系统平台
  • 贵州做网站的公司/一个产品的营销方案
  • 长沙高端网站制作公司/湖南网站建设seo
  • 手机网站判断跳转代码/免费自动推广手机软件
  • 浦口国家建设部网站/网络营销技巧培训班
  • 襄阳发布最新疫情通报/提升seo排名平台
  • 虎门网站建设多少钱/百度官方电话号码
  • 怎么做网站盗号/太原网站建设
  • 域名备案需要网站吗/百度下载软件
  • 网站建设会议验收/罗湖区seo排名
  • 刷信誉网站怎么做/网络服务提供商是指
  • 做带后台的网站/青岛seo搜索优化
  • 做响应式网站费用/外贸seo网站推广
  • 免费ppt模板哪里找/win7优化大师免安装版
  • 做动态图网站/国外网站排行
  • 南阳企业网站制作/成都网站建设团队
  • 宁夏水利建设工程网站/关键词排名提升工具
  • 网站备案怎样提交到管局/seog
  • 中小学网站建设方案/爱站关键词查询
  • 建设部安全员证书查询网站/烟台seo关键词排名