package com.iss.util;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
/**
* JSON工具类,反射的方式转换整个对象
* @author Jim Wu
*
*/
public class JSONUtil{
private static JSONUtil instance = null;
public JSONUtil(){}
/**
* 代理类时做的检查.返回应该检查的对象.
* @param bean
* @return
*/
protected Object proxyCheck(Object bean){
return bean;
}
static public String toJSONString(Object obj) throws JSONException{
return toJSONString(obj, false);
}
static public String toJSONString(Object obj, boolean useClassConvert) throws JSONException{
if(instance == null)
instance = new JSONUtil();
return instance.getJSONObject(obj, useClassConvert).toString();
}
private String getJSONArray(Object arrayObj, boolean useClassConvert) throws JSONException{
if(arrayObj == null)
return "null";
arrayObj = proxyCheck(arrayObj);
JSONArray jSONArray = new JSONArray();
if(arrayObj instanceof Collection){
Iterator iterator = ((Collection)arrayObj).iterator();
while(iterator.hasNext()){
Object rowObj = iterator.next();
if(rowObj == null)
jSONArray.put(new JSONStringObject(null));
else if(rowObj.getClass().isArray() || rowObj instanceof Collection)
jSONArray.put(getJSONArray(rowObj, useClassConvert));
else
jSONArray.put(getJSONObject(rowObj, useClassConvert));
}
}
if(arrayObj.getClass().isArray()){
int arrayLength = Array.getLength(arrayObj);
for(int i = 0; i < arrayLength; i ++){
Object rowObj = Array.get(arrayObj, i);
if(rowObj == null)
jSONArray.put(new JSONStringObject(null));
else if(rowObj.getClass().isArray() || rowObj instanceof Collection)
jSONArray.put(getJSONArray(rowObj, useClassConvert));
else
jSONArray.put(getJSONObject(rowObj, useClassConvert));
}
}
return jSONArray.toString();
}
JSONStringObject getJSONObject(Object value, boolean useClassConvert) throws JSONException{
//处理原始类型
if (value == null) {
return new JSONStringObject("null");
}
value = proxyCheck(value);
if (value instanceof JSONString) {
Object o;
try {
o = ((JSONString)value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
throw new JSONException("Bad value from toJSONString: " + o);
}
if (value instanceof Number){
return new JSONStringObject(JSONObject.numberToString((Number) value));
}
if (value instanceof Boolean || value instanceof JSONObject ||
value instanceof JSONArray) {
return new JSONStringObject(value.toString());
}
if (value instanceof String)
return new JSONStringObject(JSONObject.quote(value.toString()));
if (value instanceof Map) {
JSONObject jSONObject = new JSONObject();
Iterator iterator = ((Map)value).keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next().toString();
Object valueObj = ((Map)value).get(key);
jSONObject.put(key, getJSONObject(valueObj, useClassConvert));
}
return new JSONStringObject(jSONObject.toString());
}
//class
if(value instanceof Class)
return new JSONStringObject(JSONObject.quote(((Class)value).getSimpleName()));
//数组
if (value instanceof Collection || value.getClass().isArray()) {
return new JSONStringObject(getJSONArray(proxyCheck(value), useClassConvert));
}
return reflectObject(value, useClassConvert);
}//value.equals(null)
private JSONStringObject reflectObject(Object bean, boolean useClassConvert) {
JSONObject jSONObject = new JSONObject();
Class<? extends Object> klass = bean.getClass();
Method[] methods = klass.getMethods();
for (int i = 0; i < methods.length; i += 1) {
try {
Method method = methods[i];
String name = method.getName();
String key = "";
if (name.startsWith("get")) {
key = name.substring(3);
} else if (name.startsWith("is")) {
key = name.substring(2);
}
if (key.length() > 0 &&
Character.isUpperCase(key.charAt(0)) &&
method.getParameterTypes().length == 0) {
if (key.length() == 1) {
key = key.toLowerCase();
} else if (!Character.isUpperCase(key.charAt(1))) {
key = key.substring(0, 1).toLowerCase() +
key.substring(1);
}
Object elementObj = method.invoke(bean, null);
if(!useClassConvert && elementObj instanceof Class)
continue;
jSONObject.put(key, getJSONObject(elementObj, useClassConvert));
}
} catch (Exception e) {
/* forget about it */
}
}
return new JSONStringObject(jSONObject.toString());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jquery_ajax_json简单实例
共17个文件
class:4个
java:4个
js:2个
5星 · 超过95%的资源 需积分: 4 50 下载量 149 浏览量
2011-10-24
09:07:46
上传
评论
收藏 153KB RAR 举报
温馨提示
jquery_ajax_json简单实例jquery_ajax_json简单实例
资源推荐
资源详情
资源评论
收起资源包目录
Jquery_Ajax.rar (17个子文件)
Jquery_Ajax
src
com
iss
pojo
User.java 638B
util
JSONUtil.java 6KB
JSONStringObject.java 431B
servlet
UserServlet.java 2KB
.classpath 411B
.mymetadata 306B
.myeclipse
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
index.jsp 2KB
json.js 10KB
jquery1.4.2.js 97KB
WEB-INF
classes
com
iss
pojo
User.class 1KB
util
JSONStringObject.class 605B
JSONUtil.class 5KB
servlet
UserServlet.class 2KB
lib
org.json.jar 117KB
web.xml 656B
共 17 条
- 1
gq2010
- 粉丝: 63
- 资源: 82
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页