net.sf.json.JSONObject处理 "null" 字符串的一些坑
转:
net.sf.json.JSONObject处理 "null" 字符串的一些坑
添加依赖
<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
net.sf.json.JSONObject处理 "null" 字符串的一些坑的更多相关文章
- net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject
文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...
- net.sf.json JSONObject与JSONArray使用实例
实例自己想的一个实例应用场景:一个人可以有多个角色,例如:在家中是儿子,在学校是学生,在公司是程序员,一个人还可以办好多业务 * 每个业务好多个人都可以办,则标记(mark)就是记录这唯一标识的(如i ...
- 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject
先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...
- net.sf.json.JSONObject 和org.json.JSONObject 的差别
http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject 的差别. 一.创建jso ...
- net.sf.json.JSONObject 和org.json.JSONObject
参考 net.sf.json.JSONObject 和org.json.JSONObject 的差别
- net.sf.json JSONObject与JSONArray总结
JSONObject:json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} JSONArray:json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式 ...
- net.sf.json.JSONObject maven下载到了但是java后台一直用不了问题
需求,实体转JSON,然后用到JSONObject转JSON,但是我向下面这样引入,后台就是用不了,还是报红, <dependency> <groupId>net.sf.jso ...
- 记一次未解决的异常:java.lang.NoClassDefFoundError: net/sf/json/JSONObject
原因:Jetty会导致这个问题,Tomcat可以正常启动 一.异常产生现象 使用json-lib转换实体类/字符串,跑单元测试没问题,但是启动jetty后调用JSONArray.fromObjec ...
- Jetty+json-lib库抛异常的问题解决过程(java.lang.NoClassDefFoundError: net/sf/json/JSONObject)
一.之前抛异常是将json库改成了fastjson解决的,参见: http://www.cnblogs.com/gossip/p/5369670.html 异常信息: 二.解决步骤 ...
随机推荐
- 2.NumPy简介
一:NumPy简介 • 官网链接:http://www.numpy.org/ • NumPy教程链接:https://www.yiibai.com/numpy/ • NumPy是Python语言的一个 ...
- 数据结构---->数组
1.什么是数组? 数组是一种线性的数据结构.它同一组连续的内存空间,来存储一组具有相同类型的数据. 简单说明几点: (1).线性表:就是数据排成像一条线一样的结构.每个线性表的数据最多只有前和后两个方 ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- java线程基础巩固---同步代码块以及同步方法之间的区别和关系
在上一次中[http://www.cnblogs.com/webor2006/p/8040369.html]采用同步代码块的方式来实现对线程的同步,如下: 对于同步方法我想都知道,就是将同步关键字声明 ...
- 获取header信息
获取header信息 function _get_all_header() { // 忽略获取的header数据.这个函数后面会用到.主要是起过滤作用 $ignore = array('host',' ...
- H265码流格式
一.H265码流格式 VPS:视频参数集,用于传输视频分级信息,有利于兼容标准在可分级视频编码或多视点视频的扩展. NALU header定义: NALU header(){ Descriptor f ...
- PHP Swoole websocket协议实现
- JDK8日期处理API(转)
转载地址:http://www.cnblogs.com/comeboo/p/5378922.html 转载地址:http://blog.csdn.net/hspingcc/article/detail ...
- Codeforces 1167 F Scalar Queries 计算贡献+树状数组
题意 给一个数列\(a\),定义\(f(l,r)\)为\(b_1, b_2, \dots, b_{r - l + 1}\),\(b_i = a_{l - 1 + i}\),将\(b\)排序,\(f(l ...
- jQuery属性操作之类样式操作
类样式的操作:指对DOM属性className进行添加.移除操作.比如addClass().removeClass().toggleClass(). 1. addClass() 1.1 概述 $(se ...