一.完整案例
先定义一个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()); }
输出结果:
2. 将json数组转换为java集合对象
/** * 将json数组转换为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()); } } }
输出结果:
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()); }
输出结果:
二. 基于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);