net.sf.json.JSONObject處理 "null" 字符串的一些坑


 

轉:

net.sf.json.JSONObject處理 "null" 字符串的一些坑

版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_28988969/article/details/80168447

添加依賴

<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

示例代碼

package com.ahut; import net.sf.json.JSONObject; import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.Map; @SpringBootTest public class ConfigClientApplicationTests { @Test public void contextLoads() { JSONObject jsonObject = new JSONObject(); jsonObject.put("key", "null"); jsonObject.put("key2", "notNull"); Map itemsMap = (Map) jsonObject; System.out.println(jsonObject.get("key").getClass());//class net.sf.json.JSONNull System.out.println(jsonObject.get("key2").getClass());//class java.lang.String System.out.println(itemsMap.get("key").equals("null"));//true System.out.println("null".equals(itemsMap.get("key")));//false System.out.println(itemsMap.get("key2").equals("notNull"));//true System.out.println("notNull".equals(itemsMap.get("key2")));//true } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

運行結果:

class net.sf.json.JSONNull class java.lang.String true false true true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

代碼分析

net.sf.json.JSONObject本身就實現了Map接口:

public final class JSONObject extends AbstractJSON implements JSON, Map, Comparable { ... }
  • 1
  • 2
  • 3
  • 4

其內部維護了一個Map屬性,實際就是一個HashMap:

// 成員變量 private Map properties; // 構造方法 public JSONObject() { this.properties = new ListOrderedMap(); } public ListOrderedMap() { this(new HashMap()); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注意:

  • 非”null”字符串放到JSONObject類中時,取出來使用時是java.lang.String類型
  • “null”字符串放到JSONObject類中時,取出來的使用會轉換成net.sf.json.JSONNull類型:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package net.sf.json; import java.io.IOException; import java.io.Writer; public final class JSONNull implements JSON { private static JSONNull instance = new JSONNull(); public static JSONNull getInstance() { return instance; } private JSONNull() { } public boolean equals(Object object) { return object == null || object == this || object == instance || object instanceof JSONObject && ((JSONObject)object).isNullObject() || "null".equals(object); } public int hashCode() { return 37 + "null".hashCode(); } public boolean isArray() { return false; } public boolean isEmpty() { throw new JSONException("Object is null"); } public int size() { throw new JSONException("Object is null"); } public String toString() { return "null"; } public String toString(int indentFactor) { return this.toString(); } public String toString(int indentFactor, int indent) { StringBuffer sb = new StringBuffer(); for(int i = 0; i < indent; ++i) { sb.append(' '); } sb.append(this.toString()); return sb.toString(); } public Writer write(Writer writer) { try { writer.write(this.toString()); return writer; } catch (IOException var3) { throw new JSONException(var3); } } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

這就是為什么:

System.out.println(jsonObject.get("key").getClass());//class net.sf.json.JSONNull System.out.println(jsonObject.get("key2").getClass());//class java.lang.String
  • 1
  • 2
  • 3

itemsMap.get(“key”).equals(“null”)

分析:

JSONObject jsonObject = new JSONObject(); jsonObject.put("key", "null"); Map itemsMap = (Map) jsonObject; System.out.println(itemsMap.get("key").equals("null"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

鍵為 “key” 的值對應 “null” 字符串
所以itemsMap.get(“key”)獲取到的類型是JSONNull
所以itemsMap.get(“key”).equals(“null”)中的equals調用的是JSONNull中的equals方法

public boolean equals(Object object) { return object == null || object == this || object == instance || object instanceof JSONObject && ((JSONObject)object).isNullObject() || "null".equals(object); }
  • 1
  • 2
  • 3

所以:

System.out.println(itemsMap.get("key").equals("null"));//true
  • 1

“null”.equals(itemsMap.get(“key”))

分析:

JSONObject jsonObject = new JSONObject(); jsonObject.put("key", "null"); Map itemsMap = (Map) jsonObject; System.out.println("null".equals(itemsMap.get("key")));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

此時”null”.equals(itemsMap.get(“key”))調用的equals是String類的equals方法:


    /** * String復寫的equals方法 */ public boolean equals(Object anObject) { // 比較地址是否相同,兩個應用指向同一個對象(同一個地址) if (this == anObject) { return true; } // 判斷是否是String類 if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } // 返回結果 return false; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

執行分析:

  • itemsMap.get(“key”)獲取到的是net.sf.json.JSONNull類型
  • net.sf.json.JSONNull和”null”不是同一個對象,繼續向下執行
  • net.sf.json.JSONNull不是String類型,繼續向下執行
  • return false

所以:

System.out.println("null".equals(itemsMap.get("key")));//false


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM