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


慕容3067478
浏览 614回答 2
2回答

慕运维8079593

java to javascript:map.put("name",&nbsp;JSONNull.getInstance())javascript to hava:&nbsp;{name&nbsp;:&nbsp;undefined}或者不传name。

慕哥9229398

private&nbsp; static void each(Map m){&nbsp; &nbsp; &nbsp; &nbsp; Iterator it = m.entrySet().iterator();&nbsp; &nbsp; &nbsp; &nbsp; while (it.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map.Entry entry = (Entry) it.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object value = entry.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(value == null) continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(value instanceof List){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator lit = ((JSONArray) value).iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (lit.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object lvalue = lit.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; each((Map) lvalue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if(value instanceof Map){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; each((Map) value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if ((value instanceof net.sf.json.JSONNull)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.setValue(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java