0
点赞
收藏
分享

微信扫一扫

JSONObject构造时候不仅仅会抛出JSONException

boomwu 2022-09-05 阅读 311


一开始认为在构造json相关的对象时,如果传输的字符串参数为null,也会当作JSONException抛出,但是今天遇到了发现不是,会抛出一个NullPointerException,这里需要我们进行一下相关的处理。

如下列代码:


public void testJSONConstructor() {
final String jsonStr = null;
try {
JSONObject jsonObj = new JSONObject(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}

然而异常被捕获为NullPointerException,而不是JSONObjectException,但是为什么呢,明明如下代码声明

public JSONObject(String json) throws JSONException {
this(new JSONTokener(json));
}

对,上面的代码确实是标明了扔出JSONException,那我们看看是如何抛出这个NPE的

首先我们看JSONTokener的构造函数,带字符串参数的这个

public JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
}
this.in = in;
}


而且this.in的声明是这样的

/** The input JSON. */
private final String in;


所以如何参数in为null,则成员属性也为null

下面是JSONObject带JSONTokener的构造函数

public JSONObject(JSONTokener readFrom) throws JSONException {
/*
* Getting the parser to populate this could get tricky. Instead, just
* parse to temporary JSONObject and then steal the data from that.
*/
Object object = readFrom.nextValue();
if (object instanceof JSONObject) {
this.nameValuePairs = ((JSONObject) object).nameValuePairs;
} else {
throw JSON.typeMismatch(object, "JSONObject");
}
}


那么是哪里引起的NPE呢,JSONTokener调用的nextValue方法

public Object nextValue() throws JSONException {
int c = nextCleanInternal();
switch (c) {
case -1:
throw syntaxError("End of input");

case '{':
return readObject();

case '[':
return readArray();

case '\'':
case '"':
return nextString((char) c);

default:
pos--;
return readLiteral();
}
}


nextValue又调用的nextCleanInternal

private int nextCleanInternal() throws JSONException {
while (pos < in.length()) {
int c = in.charAt(pos++);
switch (c) {
case '\t':
case ' ':
case '\n':
case '\r':
continue;
//由于方法体过长,省略一下


所以,我们看到了之前的成员属性in 由于参数为null,没有被赋值,所以还是默认的Null,所以这里就出现了NullPointerException


所以在使用字符串构造JSONObject时,建议先检测一下字符串是否为null


举报

相关推荐

0 条评论