参考 http://www.cnblogs.com/linjiqin/archive/2011/09/19/2181408.html
在移动互联网开发的过程中,数据业务的传输一般由xml json这两种为主,json感觉在对象的使用上更加的便捷,我们就来一探究竟。
public class SimInfo {
private String imei;
private String imsi;
private String telephone;
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
public String getImsi() {
return imsi;
}
public void setImsi(String imsi) {
this.imsi = imsi;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
需要以下 jar包
1、java对象转换为json对象,并反转解析还原
SimInfo simInfo = new SimInfo();
simInfo.setTelephone("1111");
simInfo.setImei("2222");
simInfo.setImsi("3333");
String json = new JSONArray().fromObject(simInfo).toString();
System.out.println("json: " + json);
json = json.substring(1, json.length() - 1);
JSONObject obj = new JSONObject().fromObject(json);//从json对象再转为java对象
SimInfo simInfo_t = (SimInfo) JSONObject.toBean(obj, SimInfo.class);
System.out.println("obj: " + simInfo_t);
System.out.println(simInfo_t.getImei());
System.out.println(simInfo_t.getImsi());
System.out.println(simInfo_t.getTelephone());
---------------执行结果如下:
json: [{"imei":"2222","imsi":"3333","telephone":"1111"}]
obj: SimInfo@1ac04e8
2222
3333
1111
2、java list 对象转换为json对象,并反转解析还原
SimInfo simInfo = new SimInfo();
simInfo.setTelephone("1111");
simInfo.setImei("2222");
simInfo.setImsi("3333");
// java对象转换为json对象
SimInfo simInfo2 = new SimInfo();
simInfo2.setTelephone("1111x");
simInfo2.setImei("2222x");
simInfo2.setImsi("3333x");
List<SimInfo> list = new ArrayList<SimInfo>();
list.add(simInfo);
list.add(simInfo2);
String json = new JSONArray().fromObject(list).toString();
System.out.println("json: " + json);
List<SimInfo> simInfos = getJavaCollection(new SimInfo(), json);
System.out.println(simInfos.size());
for (SimInfo simInfo1 : simInfos) {
System.out.println("Imei: " + simInfo1.getImei());
System.out.println("=========");
}
private static <T> List<T> getJavaCollection(T clazz, String jsons) {
List<T> objs = null;
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(jsons);
if (jsonArray != null) {
objs = new ArrayList<T>();
List list = (List) JSONSerializer.toJava(jsonArray);
for (Object o : list) {
JSONObject jsonObject = JSONObject.fromObject(o);
T obj = (T) JSONObject.toBean(jsonObject, clazz.getClass());
objs.add(obj);
}
}
return objs;
}
---------------执行结果如下:
json: [{"imei":"2222","imsi":"3333","telephone":"1111"},{"imei":"2222x","imsi":"3333x","telephone":"1111x"}]
2
Imei: 2222
=========
Imei: 2222x
=========
3、补充实例,如果在class对象中包含存在list对象,是否还可以正常转换,答案是肯定得,见以下实例
public class SimInfo {
private String imei;
private String imsi;
private String telephone;
private List<Replay> re;
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
public String getImsi() {
return imsi;
}
public void setImsi(String imsi) {
this.imsi = imsi;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public List<Replay> getRe() {
return re;
}
public void setRe(List<Replay> re) {
this.re = re;
}
}
SimInfo simInfo = new SimInfo();
simInfo.setTelephone("1111");
simInfo.setImei("2222");
simInfo.setImsi("3333");
List<Replay> re = new ArrayList<Replay>();
re.add(new Replay("title1","name1"));
re.add(new Replay("title2","name2"));
simInfo.setRe(re);
// java对象转换为json对象
String json = new JSONArray().fromObject(simInfo).toString();
System.out.println("json: " + json);
json = json.substring(1, json.length() - 1);
JSONObject obj = new JSONObject().fromObject(json);
SimInfo simInfo_t = (SimInfo) JSONObject.toBean(obj, SimInfo.class);
System.out.println("obj: " + simInfo_t);
System.out.println(simInfo_t.getImei());
System.out.println(simInfo_t.getImsi());
System.out.println(simInfo_t.getTelephone());
System.out.println(simInfo_t.getRe().size());
---------------执行结果如下:
json: [{"imei":"2222","imsi":"3333","re":[{"name":"name1","title":"title1"},{"name":"name2","title":"title2"}],"telephone":"1111"}]
obj: SimInfo@df6ccd
2222
3333
1111
2