//使用例子,本文为原创,转载请说明出处
reads是一个多重嵌套的对象,这里不贴代码出来
String jsonEncode=JsonUtil.jsonObject_encodeToString(reads);
LogUtil.print("JsonEncode="+jsonEncode);
reads=new Reads();
reads=(Reads)JsonUtil.getObject(jsonEncode, Reads.class);
LogUtil.print("JsonDecode="+reads.toString());
运行结果:执行花费时间:20ms
05-06 13:32:35.584: I/System.out(24012): MainActivity.java(149) jsonEncode: Origin=0,0.0,null,school=School [schoolName=nanchang university, schoolTime=2008-05-16, number=123456, mile=1200, list=[Student [name=Andy, id=101, sex=true], Student [name=Andy, id=101, sex=true]]],0,list={[School [schoolName=nanchang university, schoolTime=2008-05-16, number=123456, mile=1200, list=[Student [name=Andy, id=101, sex=true], Student [name=Andy, id=101, sex=true]]], School [schoolName=nanchang university, schoolTime=2008-05-16, number=123456, mile=1200, list=[Student [name=Andy, id=101, sex=true], Student [name=Andy, id=101, sex=true]]]]},home={null}
05-06 13:32:35.588: I/System.out(24012): MainActivity.java(152) jsonEncode: JsonEncode={"id":0,"mile":0,"bonus":0,"list":[{"mile":1200,"number":123456,"list":[{"id":101,"sex":true,"name":"Andy"},{"id":101,"sex":true,"name":"Andy"}],"schoolName":"nanchang university","schoolTime":"2008-05-16"},{"mile":1200,"number":123456,"list":[{"id":101,"sex":true,"name":"Andy"},{"id":101,"sex":true,"name":"Andy"}],"schoolName":"nanchang university","schoolTime":"2008-05-16"}],"school":{"mile":1200,"number":123456,"list":[{"id":101,"sex":true,"name":"Andy"},{"id":101,"sex":true,"name":"Andy"}],"schoolName":"nanchang university","schoolTime":"2008-05-16"}}
05-06 13:32:35.602: I/System.out(24012): MainActivity.java(156) jsonEncode: JsonDecode=0,0.0,null,school=School [schoolName=nanchang university, schoolTime=2008-05-16, number=123456, mile=1200, list=[Student [name=Andy, id=101, sex=true], Student [name=Andy, id=101, sex=true]]],0,list={[School [schoolName=nanchang university, schoolTime=2008-05-16, number=123456, mile=1200, list=[Student [name=Andy, id=101, sex=true], Student [name=Andy, id=101, sex=true]]], School [schoolName=nanchang university, schoolTime=2008-05-16, number=123456, mile=1200, list=[Student [name=Andy, id=101, sex=true], Student [name=Andy, id=101, sex=true]]]]},home={null}
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtil{
//===============================JSON解析=====================================
/**
* get jsonobject
* @param String json
* @return JOSNObject
*/
public static JSONObject getJsonObject(String json){
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
} catch (JSONException e) {
e.getCause();
return null;
}
return jsonObject;
}
/**
* get jsonarray
* @param String json
* @return JOSNArray
*/
public static JSONArray getJsonArray(String json){
JSONArray jsonArray= null;
try {
jsonArray = new JSONArray(json);
} catch (JSONException e) {
//LogUtil.e(TAG, "create jsonobject exception");
e.printStackTrace();
}
return jsonArray;
}
/**
* get String data
* @param json json data
* @param key param
* @return data
* @throws JSONException
*/
public static String getString(String json,String key){
JSONObject jsonObject=getJsonObject(json);
if(jsonObject!= null){
try {
return jsonObject.getString(key);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}else{
return null;
}
}
/**
* get String data
* @param json json data
* @param key param
* @return data
* @throws JSONException
*/
public static int getInt(String json,String key){
JSONObject jsonObject=getJsonObject(json);
if(jsonObject!= null){
try {
return jsonObject.getInt(key);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
return -1;
}
return 0;
}
/**
* get Double data
* @param json json data
* @param key param
* @return data
* @throws JSONException
*/
public static double getDouble(String json,String key){
JSONObject jsonObject=getJsonObject(json);
if(jsonObject!= null){
try {
return jsonObject.getDouble(key);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0d;
}
}else{
return -1;
}
}
/**
* parent {@link Object getObject(String json,Class> c,String key)}
* @return
*/
public static Object getObject(String json,Class> c){
JSONObject jo=getJsonObject(json);
if(jo==null){
return null;
}
return getObject(jo,c);
}
/**
* This Method use in jsonObject get current class with object :{"a":"x","b":"xx"}
* @param jsonObject
* @param key query key,not null
* @param c class
* @return object
* @throws Exception
*/
public static Object getObject(String json,Class> c,String key){
JSONObject jo = null;
if(key!=null){
try {
jo = getJsonObject(json).getJSONObject(key);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(jo==null){
return null;
}
return getObject(jo,c);
}
/**
* 解析JSONObject对象,返回Object
* @param jo
* @param c
* @return Object of class c
*/
public static Object getObject(JSONObject jo,Class> c){
Object bean=null;
try {
bean = c.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Field[] fs = c.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true);
String type = f.getType().getSimpleName();//f.getGenericType();
//LogUtil.print("type="+type.toString()+","+f.getType()+",name="+f.getName());
String value = null;
try{
try{
value = jo.getString(f.getName());
}catch (JSONException e) {
value=null;
}
if(type.equals(boolean.class.getSimpleName())){
f.setBoolean(bean,Boolean.valueOf(value));
}else if(type.equals(int.class.getSimpleName())){
f.setInt(bean,Integer.valueOf(value));
}else if(type.equals(double.class.getSimpleName())){
f.setDouble(bean, Double.valueOf(value));
}else if(type.equals(long.class.getSimpleName())){
f.setLong(bean, Long.valueOf(value));
}else if(type.equals(String.class.getSimpleName())){
f.set(bean,value);
}else if(type.equals("List")){
String listClass=f.getGenericType().toString();
listClass=listClass.substring(listClass.lastIndexOf("<")+1, listClass.lastIndexOf(">"));
//LogUtil.print(listClass);
Class> listCls=null;
try {
listCls=Class.forName(listClass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(listCls!=null){
if(value==null){
f.set(bean, null);
}else{
f.set(bean, getList(value, listCls));
}
}
}else{
//f.set(bean,getBaseObject(value, f.getType()));
f.set(bean,getObject(getJsonObject(value), f.getType()));
}
}catch(NumberFormatException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}catch(IllegalArgumentException e){
e.printStackTrace();
}
}
return bean;
}
/**
* This method use in jsonObject get list object:{"b":[{"a":"x","b":"xx"},{"a":"x","b":"xx"}]}
* @param key list key,such as the "b",not null
* @param objectKey object key
* @param c object
* @return list
* @throws Exception
*/
public static List