public static JSONSerializable deSerialize(Class clz,JSONObject jsonObj) throws JSONSerializationException,JSONException{
Iterator iter=jsonObj.keys();
if (!JSONSerializable.class.isAssignableFrom(clz)){
throw new JSONSerializationException(clz+" is not an instance of "+JSONSerializable.class);
}
JSONSerializable retObj;
try {
retObj = (JSONSerializable)clz.newInstance();
} catch (Exception e1) {
throw new JSONSerializationException("trouble with no-arg instantiation of "+clz.toString()+
": "+e1.getMessage(),e1);
}
if (JSONExternalizable.class.isAssignableFrom(clz)){
((JSONExternalizable)retObj).fromJSON(jsonObj);
return retObj;
}
while(iter.hasNext()){
String key=(String)iter.next();
try {
Field f=clz.getDeclaredField(key);
if (f!=null){
f.setAccessible(true);
Class type=f.getType();
if (type.isArray()){
JSONArray array=jsonObj.getJSONArray(key);
int len=array.length();
Class cls=type.getComponentType();
Object newArray=Array.newInstance(cls, len);
for (int k=0;k<len;++k){
loadObject(newArray,cls,k,array);
}
f.set(retObj, newArray);
}
else{
loadObject(retObj,f,jsonObj);
}
}
} catch (Exception e) {
throw new JSONSerializationException(e.getMessage(),e);
}
}
return retObj;
}