Tools
----------------------------------------------------------------------------------------------------------------------------------
package cn.bb.common;
import java.util.Map;
import com.alibaba.fastjson.JSON;
{class JsonFilterEmpty public
/ **
*
*
* Description: The value is set to null Null
* @author:
*
* /
public static changeJsonVlue the Map (String JSON) {
the Map Maps = (the Map) the JSON.parse (JSON);
for (Object Map: maps.entrySet ()) {
// json determines whether the value in the string Null
IF (. ((of Map.Entry) Map) .getValue () the equals ( "")) {
// if the value is set to null null
((of Map.Entry) Map) .setValue (null);
}
}
return Maps;
}
public static changeJsonVlueDeptOrZero the Map (String JSON) {
the Map Maps = (the Map) the JSON.parse (JSON);
for ( Map Object: maps.entrySet ()) {
// json determines whether the value in the string Null
IF (((of Map.Entry) Map) .getValue (). the equals ( "") || ((of Map.Entry) Map) .getValue (). the equals (0)) {
// If the value is set to Null null
((of Map.Entry) Map) .setValue (null);
}
}
return Maps;
}
}
----------------------------------------------------------------------------------------------------------------------------------
Use Cases:
String mm = "a nice day" ;
if(Model==null) {
encapSuccessRetuMessage(mm);
}else {
String json = "{}";
try {
// Model is an entity class, which pass over the reception data, it is not output to Null field value
json = JSON.toJSONString(Model);
// call the above method of the package,
Map maps=JsonFilterEmpty.changeJsonVlue(json);
// output value is a null value json field is not output
json = JSON.toJSONString(maps);
json = json.replaceAll("\"", "\'");
} catch (Exception e) {
System.out.println(e.getMessage());
}
List<String> message4Log = new ArrayList<String>();
message4Log.add(json);
encapSuccessRetuMessage(mm,message4Log);
}
----------------------------------------------------------------------------------------------------------------------------------------------------------