就有关json与struts整合时过滤字段的笔记

在很多的应用中,有些字段是不能带到前台显示的,例如密码字段,因此需要在传到前台显示的时候需要将此字段过滤。下面就是我关于json过滤字段的方法,使用了正则表达式。

通过浏览器调试界面查看前台显示的字段中包含密码字段,如图:


该json数据包含敏感字段密码选项,故需要过滤掉。

<pre name="code" class="html"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">rows</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-array" style="color:inherit;min-width: 0px; min-height: 0px; box-sizing: border-box; position: relative; vertical-align: baseline; display: inline-block;">[,…]</span>

    1. 1:{id: 3, lastLoginTime: 1473939691479, userEmail: "121234313@qq.com", userGrade: 1, userName: "糖糖",…}
      1. id:3
      2. lastLoginTime:1473939691479
      3. userEmail:"121234313@qq.com"
      4. userGrade:1
      5. userName:"糖糖"
      6. userNtegration:8
      7. <li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userPassword:"123456"</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;"></span></li>
      8. userPhone:"18949344290"
      9. userRegistdate:1471936892615
      10. userSex:1
      11. userStatus:0

 

通过观察分析可知,rows是一个数组的结构,利用正则表达式过滤时应该这样做

<result name="jsonMap" type="json">
        <param name="root">jsonMap</param>
        <!-- 过滤用户密码字段 -->
        <param name="excludeProperties">
             rows\[\d+\]\.userPassword,
        </param>
  </result>

如果不知道正则表达是的意思,请翻阅正则表达式api。这样就可以将rows下的userPassword字段过滤掉了。加上这个配置后,再次查询数据,结果显示如下:

  <pre name="code" class="html"><ol class="children expanded" style="min-width: 0px; min-height: 0px; box-sizing: border-box; list-style-type: none; padding-left: 12px; display: block; color: rgb(33, 33, 33); font-family: Consolas, "Lucida Console", monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><li class="parent expanded" style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">rows</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-array" style="color:inherit;min-width: 0px; min-height: 0px; box-sizing: border-box; position: relative; vertical-align: baseline; display: inline-block;">[,…]</span></li><ol class="children expanded" style="min-width: 0px; min-height: 0px; box-sizing: border-box; list-style-type: none; padding-left: 12px; display: block;"><li class="parent expanded" style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">1</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-object" style="color:inherit;min-width: 0px; min-height: 0px; box-sizing: border-box; position: relative; vertical-align: baseline; display: inline-block;">{id: 3, lastLoginTime: 1473939691479, userEmail: "121234313@qq.com", userGrade: 1, userName: "糖糖",…}</span></li><ol class="children expanded" style="min-width: 0px; min-height: 0px; box-sizing: border-box; list-style-type: none; padding-left: 12px; display: block;"><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">id</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">3</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">lastLoginTime</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1473939691479</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userEmail</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-string" style="color:#c41a16;min-width: 0px; min-height: 0px; box-sizing: border-box; white-space: pre; unicode-bidi: -webkit-isolate;">"121234313@qq.com"</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userGrade</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userName</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-string" style="color:#c41a16;min-width: 0px; min-height: 0px; box-sizing: border-box; white-space: pre; unicode-bidi: -webkit-isolate;">"糖糖"</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userNtegration</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">8</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userPhone</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-string" style="color:#c41a16;min-width: 0px; min-height: 0px; box-sizing: border-box; white-space: pre; unicode-bidi: -webkit-isolate;">"18949344290"</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userRegistdate</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1471936892615</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userSex</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userStatus</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">0</span></li></ol></ol></ol>

 


密码字段成功的过滤掉了。这也是我刚刚学过得,分享出来,有不足之处欢迎点出!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值