最近项目中需要在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
;
}
}