json字符串让里面的""和null不显示出来

工具类

----------------------------------------------------------------------------------------------------------------------------------

package cn.bb.common;

import java.util.Map;

import com.alibaba.fastjson.JSON;

public class JsonFilterEmpty {
/**
*
*
* 功能描述:将value为空的设置为Null
* @author:
*
*/
public static Map changeJsonVlue(String json) {
Map maps = (Map)JSON.parse(json);
for (Object map : maps.entrySet()){
//判断json字符串里的value是否为Null
if(((Map.Entry)map).getValue().equals("")) {
//如果为Null将值设置为Null
((Map.Entry)map).setValue(null);
}
}
return maps;
}
public static Map changeJsonVlueDeptOrZero(String json) {
Map maps = (Map)JSON.parse(json);
for (Object map : maps.entrySet()){
//判断json字符串里的value是否为Null
if(((Map.Entry)map).getValue().equals("")||((Map.Entry)map).getValue().equals(0)) {
//如果为Null将值设置为Null
((Map.Entry)map).setValue(null);
}
}
return maps;
}

}

----------------------------------------------------------------------------------------------------------------------------------

使用案例:

String mm="今天天气真好";

if(Model==null) {

encapSuccessRetuMessage(mm);

}else {

String json = "{}";

try {

//Model是一个实体类,里面的数据前台传过来的,它不输出value为Null字段

json = JSON.toJSONString(Model);

//调用上面封装的方法,

Map maps=JsonFilterEmpty.changeJsonVlue(json);

//输出一个json值为value为null的字段,不输出

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);

}

----------------------------------------------------------------------------------------------------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/manager222/p/11973802.html