最近項目中需要在android中將對象轉為json字串以及還原序列化為對象,查閱了android sdk文檔後,發現android中沒有類似C#裡DataContractJsonSerializer的類,想想需求也不算複雜,就自己寫了兩個方法解決,代碼如下:
public static String getJson(Object objSource){
try{
Object object = getJsonObject(objSource);
if(object == null)
return "";
return ((JSONObject)object).toString();
}catch(Exception err){
Log.e(TAG, "getJson exception:"+err.getMessage());
return "";
}
}
private static Object getJsonObject(Object objSource) throws Exception{
if(objSource != null){
Class<?> objClass = objSource.getClass();
if(objClass.getPackage().getName().startsWith(PackageString)){
JSONObject jsonObject = new JSONObject();
Field[] publicFields = objClass.getFields();
Object val;
for(Field field : publicFields){
val = field.get(objSource);
if(val != null){
Class<?> valClass = val.getClass();
if(valClass.isArray()){
JSONArray array = new JSONArray();
int length = Array.getLength(val);
for(int i = 0; i<length; i++){
array.put(getJsonObject(Array.get(val, i)));
}
jsonObject.put(field.getName(), array);
}else if(valClass.equals(ArrayList.class)){
ArrayList<?> aList = ArrayList.class.cast(val);
Iterator<?> iterator = aList.iterator();
JSONArray array = new JSONArray();
while (iterator.hasNext()) {
array.put(getJsonObject(iterator.next()));
}
jsonObject.put(field.getName(), array);
}else{
jsonObject.put(field.getName(),getJsonObject(val));
}
}else{
jsonObject.put(field.getName(), null);
}
}
return jsonObject;
}else{
return objSource;
}
}
return null;
}
public static Object parseJson(Class<?> targetClass,String jsonString){
try{
Object object = targetClass.newInstance();
JSONObject jsonObject = new JSONObject(jsonString);
String key;
Class<?> fieldClass;
Object element;
Class<?> elementType;
Field[] fields = targetClass.getFields();
for(Field field : fields){
key = field.getName();
fieldClass = field.getType();
if(fieldClass.equals(ArrayList.class)){
String genericType = field.getGenericType().toString();
if(genericType.indexOf("<") > 0){
genericType = genericType.substring(genericType.indexOf("<")+1,genericType.lastIndexOf(">"));
}
ArrayList<Object> aList = new ArrayList<Object>();
JSONArray array = jsonObject.getJSONArray(key);
int length = array.length();
if(genericType.startsWith(PackageString)){
elementType = Class.forName(genericType);
Field[] elementFields = elementType.getFields();
JSONObject subJsonObject;
for(int i=0;i<length;i++){
element = elementType.newInstance();
subJsonObject = array.getJSONObject(i);
for(Field subField : elementFields){
subField.set(element, subJsonObject.get(subField.getName()));
}
aList.add(element);
}
}else{
for(int i=0;i<length;i++){
aList.add(array.get(i));
}
}
field.set(object, aList);
}else if(fieldClass.isArray()){
JSONArray array = jsonObject.getJSONArray(key);
int length = array.length();
String arrayTypeName = fieldClass.getComponentType().getName();
Object[] objArray = new Object[length];
if(arrayTypeName.startsWith(PackageString)){
elementType = Class.forName(arrayTypeName);
Field[] elementFields = elementType.getFields();
JSONObject subJsonObject;
for(int i=0;i<length;i++){
element = elementType.newInstance();
subJsonObject = array.getJSONObject(i);
for(Field subField : elementFields){
subField.set(element, subJsonObject.get(subField.getName()));
}
objArray[i] = element;
}
}else{
for(int i=0;i<length;i++){
objArray[i] = array.get(i);
}
}
field.set(object, objArray);
}else{
field.set(object, jsonObject.get(key));
}
}
return object;
}catch(Exception err){
Log.e(TAG, "parseJson exception:"+err.getMessage());
return null;
}
}
支援基本類型、數組、ArrayList成員組成的類