转:

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

2018年05月02日 16:41:25 大白能 阅读数:7026
 
版权声明:本文为博主原创文章,未经博主允许不得转载。 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

最新文章

  1. 吉日嘎拉C#快速开发平台V4.0到V4.2升级记
  2. gulp教程之gulp-concat
  3. js上传压缩图片
  4. Instruments_Activity Monitor使用入门
  5. 【JS】js获得下拉列表选中项的值和id
  6. mongoDB 插入数据 用java实现
  7. php 目录及文件操作
  8. poj2406 Power Strings(kmp失配函数)
  9. Java GC 日志详解(转)
  10. 初学杂文 String类
  11. Triangle Problems
  12. APP被苹果App Store拒绝的79个原因【转】
  13. 如何下载最新Xshell版本、免费官方正版软件的技巧过程
  14. SQL修改某个字段中某相同部分(MySQL)
  15. java 日志体系(三)log4j从入门到详解
  16. 某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
  17. Python—字典的操作
  18. 廖雪峰Java1-4数组操作-5命令行参数
  19. 定时调度系列之Quartz.Net详解(转)
  20. JavaScript进阶系列05,事件的执行时机, 使用addEventListener为元素同时注册多个事件,事件参数

热门文章

  1. 制作CentOS8安装U盘时遇到的“Minimal BASH-like...”问题
  2. 4.NIO_Channel 通道
  3. DBSCAN算法及sklearn实现
  4. 获取header信息
  5. Linux命令手册man
  6. 关于div的水平垂直居中
  7. jsp模糊查询
  8. 题解 【NOIP2014】解方程
  9. vue实例之组件开发:图片轮播组件
  10. Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)