首页 > json 中的 null 转换为 java 中的null

json 中的 null 转换为 java 中的null

使用的是json-lib jar包,ssh 框架。 在项目中我们需要json数据格式传入action处理,有一些值为空的。可是我在测试的时候。这样的方式到服务器中取到的值就是字符串"null"了。如果写上map.put("name","");。到服务器就是空字符串。我想达到那种取出来就是 null 的效果,怎么做出来

Map<String,String> map = new HashMap<String,String>();
map.put("name",null);


java to javascript:

map.put("name", JSONNull.getInstance())

javascript to hava:

 {name : undefined}
  

或者不传name。


不要传值,后台如果用model接收,自然对应的值就是null。


private  static void each(Map m){
        Iterator it = m.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Entry) it.next();
            Object value = entry.getValue();
            if(value == null) continue;
            if(value instanceof List){
                System.err.println(1);
                Iterator lit = ((JSONArray) value).iterator();
                while (lit.hasNext()) {
                    Object lvalue = lit.next();
                    each((Map) lvalue);
                }
            }else if(value instanceof Map){
                each((Map) value);
            }else if ((value instanceof net.sf.json.JSONNull)){
                entry.setValue(null);
            }
        }
    }

换成另外的代号,比如http_0 接收的时候判断一下值,如果是http_0 的话就输出 null

【热门文章】
【热门文章】