json学习系列(8)JSON与JAVA数据的相互转换实例

时间:2023-12-12 23:27:32

一.完整案例

先定义一个java实体对象,如下:

package com.pcitc.json.cnblog;

/**
* SimInfo实体对象
*
* @Description
* @author admin
* @created 2015年6月30日 上午10:23:21
* @version
* @history
* @see
*/
public class SimInfo { private String addTime;
private String iccid;
private int id;
private String imei;
private String imsi;
private String phoneType;
private String remark;
private String tel; public String getAddTime() {
return addTime;
} public void setAddTime(String addTime) {
this.addTime = addTime;
} public String getIccid() {
return iccid;
} public void setIccid(String iccid) {
this.iccid = iccid;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} 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 getPhoneType() {
return phoneType;
} public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
} public String getRemark() {
return remark;
} public void setRemark(String remark) {
this.remark = remark;
} public String getTel() {
return tel;
} public void setTel(String tel) {
this.tel = tel;
} }

1. json对象转换为java对象

@Test
public void jsonToJava() {
String json = "[{\"addTime\":\"2011-09-19 14:23:02\",\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]";
// 接收{}对象,此处接收数组对象会有异常
if (json.indexOf("[") != -1) {
json = json.replace("[", "");
}
if (json.indexOf("]") != -1) {
json = json.replace("]", "");
}
// 把字符串形式的JSON串转换成JSON对象
JSONObject obj = JSONObject.fromObject(json);
// 把JSON对象转成java对象
SimInfo simInfo = (SimInfo) JSONObject.toBean(obj, SimInfo.class);
// 输出java对象的相关属性
System.out.println(simInfo.getAddTime());
System.out.println(simInfo.getIccid());
System.out.println(simInfo.getImei());
System.out.println(simInfo.getImsi());
System.out.println(simInfo.getPhoneType());
System.out.println(simInfo.getRemark());
System.out.println(simInfo.getTel());
System.out.println(simInfo.getId());
}

输出结果:

aaarticlea/png;base64,iVBORw0KGg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组转换为java集合对象
*
* @Description
* @author admin
* @created 2015年6月30日 上午10:10:15
*/
@Test
public void jsonToJavaCollections() {
String jsons = "[{\"addTime\":\"2011-09-19 14:23:02\",\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"},"
+ "{\"addTime\":\"2011-11-11 14:23:02\",\"iccid\":\"2222\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]";
// 把字符串形式的JSON串转换成JSON数组对象
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(jsons);
if (jsonArray != null) {
// 把JSON数组对象转换成java集合对象
List<Object> list = (List<Object>) JSONSerializer.toJava(jsonArray);
for (Object o : list) {
// 把java对象转换成JSON对象
JSONObject jsonObject = JSONObject.fromObject(o);
// 把JSON对象转换成特定的java对象
SimInfo simInfo = (SimInfo) JSONObject.toBean(jsonObject,
new SimInfo().getClass());
// 输出java对象属性
System.out.println("addTime: " + simInfo.getAddTime());
}
}
}

输出结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAA7CAIAAACFYMRLAAAEvUlEQVR4nO2cTY7bMAxGfS4fyOfxaYwuZl8UmP0svClQ9AK9gLtwYuuHlClHSczBe8iirRySFvXZlBqp+/P768fPv7++/n2/z/TxuUDA9PH59qTwafvpVgFPH5/f8vNuyVyLt6eDT/PPTcDvHloAcAYEDOAYBAzgGAQM4BgEDOAYBAzgmJYCnoauG6baJuXqrh/nJlEBfGNeLuB57LucWK0XEPA0KLEdtN3vT+qHQtOJOMKOrDKphdHcoKU1R7vjZOBURKj3YTGTPnjfG3ge+6v22jz2W7Tz2EfZnYb9r3LbOAjjq9Cko/sKW9Ymm1U1jOYGl91U35vNlXpeDdcQoNSHVl/XBgEfEY3mNOiwcRrWP02iPLSmsmPZVyawcCwWUMNobjC+hboJlB5V7NlispQ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l92pI5rgyFmAXOkTjX8FhrAMQgYwDEIGMAxCBjAMQcC7rruldEAQBUIGMAxCBjAMQgYwDEIGMAxCBjAMQgYwDEIGMAxCBjAMQgYwDEIGMAxCBjAMQgYwDEIGMAxCBjAMQgYwDEIGMAx/wGSUp/KOd+aRAAAAABJRU5ErkJggg==" alt="" />

3. java对象转换成为JSON对象

/**
* java对象转换成为JSON对象
*
* @Description
* @author admin
* @created 2015年6月30日 上午10:31:08
*/
@Test
public void javaToJson() {
SimInfo simInfo = new SimInfo();
simInfo.setAddTime("2015-06-30");
simInfo.setIccid("1111");
simInfo.setImei("2222");
simInfo.setImsi("3333");
simInfo.setPhoneType("4");
simInfo.setRemark("aaaa");
simInfo.setTel("5555");
// java对象转换为json数组对象
JSONArray jsonArray = new JSONArray().fromObject(simInfo);
// json:
// [{"addTime":"2011-09-19 14:23:02","iccid":"1111","id":0,"imei":"2222","imsi":"3333","phoneType":"4444","remark":"aaaa","tel":"5555"}]
System.out.println("json: " + jsonArray.toString());
}

输出结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABK4AAABRCAIAAAB0c3n3AAAS5UlEQVR4nO2dO87suBFGe1v9r8Kr0Ha6s0m9CsHBOHBkGJh8AiUGBs4cGAYct4N+iEVW8SVSj9Y5UHBvUyKLxSJZH6WZe3k8Hn/567/+8+9/fq4//vn7X/7+r3/8/t/vu8Zff3uAw/jrb5sPChcXFxcXFxcXF1fO9adf/vjlb/9bcv3plz+aVLK5K5pclwdS8MQgBbm4uLi4uLi4uI5yPYXcHq7NXdHkQgqeGqQgFxcXFxcXFxcX1zkvUwqOv/72ldfW4mtfbD4cXFxcXFxcXFxcXFybXKYU3FqkAAAAAAAAQC+QggAAAAAAAKcDKQgAAAAAAHA6kIIAAAAAAACnAykIAAAAAABwOvYqBafb9XK9TT2bGIfeLfSiq+UreP6I4JaQ2SfjcBnGrc1RsEZtnkHT7bpP01UMa4uCk0hWqQzh6Xa9aFTFlFVZurao9dPtyoADAIABU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/VW0HlRut/QSX3P9y5HEEI/av/bIujIoQfl0MYfGjy/FTWe3z63ivJJT1Qbd258CmbKsWC8uvLSS6p/3Y9ODygFAQAAAAAA4MggBQEAAAAAAE4HUhAAAAAAAOB0IAUBAAAAAABOB1IQAAAAAADgdCAFAQAAAAAATgdSEAAAAAAA4HQgBQEAAAAAAE4HUhAAAAAAAOB0IAUBAAAAAABOB1IQAAAAAADgdCAFAQAAAAAATgdSEAAAAAAA4HQgBQEAAAAAAE4HUhAAAAAAAOB0IAUBAAAAAABOB1IQAAAAAADgdCAFAQAAAAAATgdSEAAAAAAA4HQgBQEAAAAAAE4HUhAAAAAAAOB0IAUBAAAAAABOB1IQAAAAAADgdCAFAQAAAAAATgdSEAAAAAAA4HQgBQEAAAAAAE4HUhAAAAAAAOB0IAUBAAAAAABOB1IQAAAAAADgdCAFAQAAAAAATgdSEAAAAAAA4HQgBQEAAAAAAE4HUhAAAAAAAOB0IAUBAAAAAABOB1IQAAAAAADgdCAFAQAAAAAATgdSEAAAAAAA4HQgBQEAAAAAAE7H/wFSJcwM1504uAAAAABJRU5ErkJggg==" alt="" />

二. 基于json-lib.jar包Json实例程序的一些总结

1. 把一个JSON字符串转换成JSON对象的两种方式:

String json = "[{\"addTime\":\"2011-09-19 14:23:02\",\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]";
if (json.indexOf("[") != -1) {
json = json.replace("[", "");
}
if (json.indexOf("]") != -1) {
json = json.replace("]", "");
}

方式一:

JSONObject obj = JSONObject.fromObject(json);

方式二:

JSONObject obj = (JSONObject) JSONSerializer.toJSON(json);

2. JSONObject to DynaBean

String json = "{name=\"json\",bool:true,int:1,double:2.2}";
JSONObject jsonObject = JSONObject.fromObject(json);
//抽象的写法:DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject );
Object bean = JSONObject.toBean(jsonObject);
//Object bean1 = JSONSerializer.toJava(jsonObject);

3. JSONObject to JavaBean

String json = "{name:\"zhangsan\",age:25,hight:1.72,sex:true}";
JSONObject jsonObject = JSONObject.fromObject(json);
UserBean bean = (UserBean) JSONObject.toBean(jsonObject, UserBean.class);

4. JSONArray to List

String json = "[\"first\",\"second\"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
List output = (List) JSONSerializer.toJava(jsonArray);

5. JSONArray to array

String json = "[\"first\",\"second\"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
Object[] output = (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig);