文章待補充,先寫寫如下知識點好了。web
JSON做爲一個輕量級的文本數據交換格式經常用於web後臺的數據封裝和傳輸。JSON及其工具包給開發帶來很是多的好處,提升了開發效率。然而,世間老是免不了存在一些坑,吾輩需笑看人生路,潛心研碼。NULL值做爲一個特殊狀況,在處理的時候尤爲須要當心處理。不幸的是,隨着咱們使用的工具類的做者的不一樣,對NULL的處理也有不一樣。此處,就扒一扒如題兩家JSONObject工具類對NULL的處理。json
問題來了,這兩家對NULL是如何處理的,示例代碼以下:數組
String resp = "{" + " \"id\": \"123456fdae23\",\n" + " \"userName\": \"dodoro\",\n" + " \"cnName\": \"清風明月多多愛工做\",\n" + " \"anull\": null,\n" + " \"anothernull\": null,\n" + "}"; JSONObject jsonObject = JSONObject.fromObject(resp); if(jsonObject.getString("anull").equals("null")){ System.out.println("null became a string"); } com.alibaba.fastjson.JSONObject userInfo = com.alibaba.fastjson.JSONObject.parseObject(resp); if(userInfo.getString("anull") == null){ System.out.println("alibaba NB, null is a null"); } System.out.println(jsonObject);
運行的測試結果以下:app
null became a string alibaba NB, null is a null
結果很明顯,net.sf.json.JSONObject
將null轉換成了一個字符串"null"
,com.alibaba.fastjson
則是將null轉換爲了null
,二者是有不一樣的。工具
首先,net.sf.json.JSONObject
提供了一個將其餘類型的數據轉換爲轉換爲JSONObject
的方法fromObject()
測試
public static JSONObject fromObject(Object object) { return fromObject(object, new JsonConfig()); } public static JSONObject fromObject(Object object, JsonConfig jsonConfig) { if(object != null && !JSONUtils.isNull(object)) { if(object instanceof Enum) { //是不是枚舉類型 throw new JSONException("\'object\' is an Enum. Use JSONArray instead"); } else if(!(object instanceof Annotation) && (object == null || !object.getClass().isAnnotation())) { //是不是註解類型 if(object instanceof JSONObject) { //是否是JSONObject return _fromJSONObject((JSONObject)object, jsonConfig); } else if(object instanceof DynaBean) { return _fromDynaBean((DynaBean)object, jsonConfig); } else if(object instanceof JSONTokener) { return _fromJSONTokener((JSONTokener)object, jsonConfig); } else if(object instanceof JSONString) { //是否是符合JSON格式的字符串 return _fromJSONString((JSONString)object, jsonConfig); } else if(object instanceof Map) { //是否是Map return _fromMap((Map)object, jsonConfig); } else if(object instanceof String) { //是否是字符串 return _fromString((String)object, jsonConfig); } else if(!JSONUtils.isNumber(object) && !JSONUtils.isBoolean(object) && !JSONUtils.isString(object)) { if(JSONUtils.isArray(object)) { //是否是數組 throw new JSONException("\'object\' is an array. Use JSONArray instead"); } else { return _fromBean(object, jsonConfig); //從對象轉換爲JSONObject,須要知足Bean的getter和setter } } else { return new JSONObject(); } } else { throw new JSONException("\'object\' is an Annotation."); } } else { return new JSONObject(true); } }
從其源代碼能夠看出net.sf.json
功能強大,接口簡潔,然而主要看的並非這裏,而是它的_fromJSONString((JSONString)object, jsonConfig);
方法。this
private static JSONObject _fromString(String str, JsonConfig jsonConfig) { if(str != null && !"null".equals(str)) { return _fromJSONTokener(new JSONTokener(str), jsonConfig); } else { fireObjectStartEvent(jsonConfig); fireObjectEndEvent(jsonConfig); return new JSONObject(true); } } private static JSONObject _fromJSONTokener(JSONTokener tokener, JsonConfig jsonConfig) { try { if(tokener.matches("null.*")) { fireObjectStartEvent(jsonConfig); fireObjectEndEvent(jsonConfig); return new JSONObject(true); } else if(tokener.nextClean() != 123) { throw tokener.syntaxError("A JSONObject text must begin with \'{\'"); } else { fireObjectStartEvent(jsonConfig); Collection exclusions = jsonConfig.getMergedExcludes(); PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter(); JSONObject jsonObject = new JSONObject(); while(true) { char jsone = tokener.nextClean(); switch(jsone) { case '\u0000': throw tokener.syntaxError("A JSONObject text must end with \'}\'"); case '}': fireObjectEndEvent(jsonConfig); return jsonObject; default: tokener.back(); String key = tokener.nextValue(jsonConfig).toString(); //此處取得一個token jsone = tokener.nextClean(); if(jsone == 61) { if(tokener.next() != 62) { tokener.back(); } } else if(jsone != 58) { throw tokener.syntaxError("Expected a \':\' after a key"); } char peek = tokener.peek(); boolean quoted = peek == 34 || peek == 39; Object v = tokener.nextValue(jsonConfig); //獲取當前key的value if(!quoted && JSONUtils.isFunctionHeader(v)) { String params = JSONUtils.getFunctionParams((String)v); //轉換爲String嫋 //省略..... }
nextValue
方法是JSONTokener
類的方法,代碼以下:spa
public Object nextValue(JsonConfig jsonConfig) { char c = this.nextClean(); switch(c) { case '\"': case '\'': return this.nextString(c); case '[': this.back(); return JSONArray.fromObject(this, jsonConfig); case '{': this.back(); return JSONObject.fromObject(this, jsonConfig); default: StringBuffer sb = new StringBuffer(); char b; for(b = c; c >= 32 && ",:]}/\\\"[{;=#".indexOf(c) < 0; c = this.next()) { sb.append(c); } this.back(); String s = sb.toString().trim(); if(s.equals("")) { throw this.syntaxError("Missing value."); } else if(s.equalsIgnoreCase("true")) { return Boolean.TRUE; } else if(s.equalsIgnoreCase("false")) { return Boolean.FALSE; } else if(!s.equals("null") && (!jsonConfig.isJavascriptCompliant() || !s.equals("undefined"))) { if((b < 48 || b > 57) && b != 46 && b != 45 && b != 43) { if(!JSONUtils.isFunctionHeader(s) && !JSONUtils.isFunction(s)) { switch(this.peek()) { case ',': case '[': case ']': case '{': case '}': throw new JSONException("Unquotted string \'" + s + "\'"); default: return s; } } else { return s; } } else { if(b == 48) { if(s.length() > 2 && (s.charAt(1) == 120 || s.charAt(1) == 88)) { try { return new Integer(Integer.parseInt(s.substring(2), 16)); } catch (Exception var13) { ; } } else { try { return new Integer(Integer.parseInt(s, 8)); } catch (Exception var12) { ; } } } try { return new Integer(s); } catch (Exception var11) { try { return new Long(s); } catch (Exception var10) { try { return new Double(s); } catch (Exception var9) { return s; } } } } } else { return JSONNull.getInstance(); //返回一個JSONNUll對象 } } } public final class JSONNull implements JSON { public String toString() { return "null"; //返回一個「null」字符串 } public String toString(int indentFactor) { return this.toString(); } }
至此能夠看到net.sf.json
將NULL轉換爲一個「null」 字符串處理。若是直接對存在null值得JSON對象使用getString
進行操做,那麼就容易出現使用getString("anull") == null
的操做,而fastjson
則是將null直接轉換爲一個null
對象,因此能夠直接使用getString("anull") == null
進行比較。code
萬幸的是,若是咱們經過JSON的方法將JSON對象轉換爲封裝相同字段的JAVA
對象,不管是哪一種JSON
工具包,最後都會把NULL轉換爲null
。對象
因此建議後臺獲取的JSON對象,若是想要使用,最好先轉換爲JAVABean來避免不一樣工具包之間的差別所帶來的異常故障。