JSONObjectorg.json的一点修改

修改org.json.JSONObject的stringTovalue,返回能容纳整数的最小包装类型而不是Integer。

(修正数据交互工具中当对象包含属性的类型为类型为byte/short时反射调用field.set(bean,obj)引发异常。)

黑色粗体斜体为增加部分,修改代码如下:

static public Object stringTovalue(String s) {
if (s.equals("")) {
return s;
}
if (s.equalsIgnoreCase("true")) {
return Boolean.TRUE;
}
if (s.equalsIgnoreCase("false")) {
return Boolean.FALSE;
}
if (s.equalsIgnoreCase("null")) {
return JSONObject.NULL;
}

/*
* If it might be a number,try converting it.
* We support the non-standard 0x- convention.
* If a number cannot be produced,then the value will just
* be a string. Note that the 0x-,plus,and implied string
* conventions are non-standard. A JSON parser may accept
* non-JSON forms as long as it accepts all correct JSON forms.
*/

char b = s.charat(0);
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
if (b == '0' && s.length() > 2 &&
(s.charat(1) == 'x' || s.charat(1) == 'X')) {
try {
return new Integer(Integer.parseInt(s.substring(2),16));
} catch (Exception ignore) {
}
}
try {
if (s.indexOf('.') > -1 ||
s.indexOf('e') > -1 || s.indexOf('E') > -1) {
return Double.valueOf(s);
} else {
Long myLong = new Long(s);
if (myLong.shortValue() == myLong.byteValue()) {
return new Byte(myLong.byteValue());
} if (myLong.intValue() == myLong.shortValue()) {
return new Short(myLong.shortValue());
}
if (myLong.longValue() == myLong.intValue()) {
return new Integer(myLong.intValue());
} else {
return myLong;
}
}
} catch (Exception ignore) {
}
}
return s;
}

还有org.json.JSONObject中增加

/** * Put a key/int pair in the JSONObject. * * @param key A key string. * @param value An int which is the value. * @return this. * @throws JSONException If the key is null. */ public JSONObject put(String key,byte value) throws JSONException { put(key,new Byte(value)); return this; } /** * Put a key/int pair in the JSONObject. * * @param key A key string. * @param value An int which is the value. * @return this. * @throws JSONException If the key is null. */ public JSONObject put(String key,short value) throws JSONException { put(key,new Short(value)); return this; }

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...