自己写一个子类继承自JSONObject ,然后重写它的getString(String name)方法: /** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary. * * @throws JSONException if no such mapping exists. */ public String getString(String name) throws JSONException { Object object = get(name); String result = JSON.toString(object); if (result == null) { throw JSON.typeMismatch(name, object, "String"); } return result; } 代码中看到当object为null的时候,会直接JSON.toString(object),返回就是"null"了,想要控制,就在当object为null对象的时候直接返回""就行了,可以不用一一去处理了
1、保存读取数据库的时候判断;2、生成json后对json遍历判断;3、前台显示de时候判断。
public String getAddress(){
if(address==null){
return "";
}
return address;
}
.....
/**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary.
*
* @throws JSONException if no such mapping exists.
*/
public String getString(String name) throws JSONException {
Object object = get(name);
String result = JSON.toString(object);
if (result == null) {
throw JSON.typeMismatch(name, object, "String");
}
return result;
} 代码中看到当object为null的时候,会直接JSON.toString(object),返回就是"null"了,想要控制,就在当object为null对象的时候直接返回""就行了,可以不用一一去处理了