fastjson的值過濾器ValueFilter


https://blog.csdn.net/linyifan_/article/details/83060408

項目中需要將前端傳進的json數據清空值前后空格

兩種實現方法

1.土方法 迭代trim()

  1.  
    RequestContext context = RequestContext.getCurrentContext();
  2.  
    InputStream in = (InputStream) context.get( "requestEntity");
  3.  
    String body = StreamUtils.copyToString(in, Charset.forName( "UTF-8"));
  4.  
    JSONObject object = JSON.parseObject(body);
  5.  
    if (object == null) object = new JSONObject();
  6.  
    jsonParameterTrimObject(object);
  7.  
     
  8.  
    /**
  9.  
    * 清空JSONObject 值前后空格
  10.  
    * @param object
  11.  
    */
  12.  
    private void jsonParameterTrimObject(JSONObject object){
  13.  
    for(String str: object.keySet()){
  14.  
    Object o = object.get(str);
  15.  
    if(null != o){
  16.  
    if(o instanceof String){ //值為字符串類型
  17.  
    object.put(str,((String) o).trim()); //清空值前后空格
  18.  
    }
  19.  
    if(o instanceof JSONObject){ //值為JSON對象
  20.  
    jsonParameterTrimObject((JSONObject)o);
  21.  
    }
  22.  
    if(o instanceof JSONArray) { //值為JSON數組
  23.  
    jsonParameterTrimArray((JSONArray)o);
  24.  
    }
  25.  
    }
  26.  
    }
  27.  
    }
  28.  
     
  29.  
    /**
  30.  
    * 清空JSONArray 值前后空格
  31.  
    * @param array
  32.  
    */
  33.  
    private void jsonParameterTrimArray(JSONArray array){
  34.  
    if(array.size() > 0){
  35.  
    for(int i=0; i< array.size();i++){
  36.  
    Object oa = array.get(i);
  37.  
    if(null != oa){
  38.  
    if(oa instanceof String){ //值為字符串類型
  39.  
    array.set(i,((String) oa).trim()); //清空值前后空格
  40.  
    }
  41.  
    if(oa instanceof JSONObject){ //值為JSON對象
  42.  
    jsonParameterTrimObject((JSONObject)oa);
  43.  
    }
  44.  
    if(oa instanceof JSONArray) { //值為JSON數組
  45.  
    jsonParameterTrimArray((JSONArray)oa);
  46.  
    }
  47.  
    }
  48.  
    }
  49.  
    }
  50.  
    }

2.使用fastJson 值過濾器

  1.  
    package cango.scf.com.filter;
  2.  
     
  3.  
    import com.alibaba.fastjson.serializer.ValueFilter;
  4.  
     
  5.  
    public class SimpleValueFilter implements ValueFilter {
  6.  
    @Override
  7.  
    public Object process(Object object, String name, Object value) {
  8.  
    if (value instanceof String) {
  9.  
    value = ((String) value).trim();
  10.  
    }
  11.  
    return value;
  12.  
    }
  13.  
    }
  14.  
     
  15.  
    RequestContext context = RequestContext.getCurrentContext();
  16.  
    InputStream in = (InputStream) context.get( "requestEntity");
  17.  
    if (in == null) {
  18.  
    in = context.getRequest().getInputStream();
  19.  
    }
  20.  
    String body = StreamUtils.copyToString(in, Charset.forName( "UTF-8"));
  21.  
    JSONObject object = JSON.parseObject(body);
  22.  
    if (object == null) object = new JSONObject();
  23.  
     
  24.  
    body = JSON.toJSONString(object, new SimpleValueFilter());

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM