本文使用json-lib jar包實現Json與bean的相互轉換
1.將字符串轉為JSON
使用JSONObject.fromObject(str)方法即可將字符串轉為JSON對象
使用JSONObject.put("attribute","value")可為JSON添加屬性
如果需要轉為JSON數組,只需使用JSONArray對象提供的方法即可
/** * 一些簡單的轉換 */ public static void transformStringTest() { String str = "{" + "\"id\":" + "\"1\"," + "\"name\":" + "\"zhangsan\"" + "}"; //1.將字符串轉為JSON JSONObject jsonObj = JSONObject.fromObject(str); System.out.println(jsonObj.toString()); //JSON添加屬性 jsonObj.put("age", "22"); System.out.println(jsonObj.toString()); //2.將對象轉為數組 JSONArray jsonArr = JSONArray.fromObject(jsonObj); System.out.println(jsonArr.toString()); //3.將數組添加到JSON對象中 JSONObject obj = new JSONObject(); obj.put("employees", jsonArr); System.out.println(obj.toString()); }
/* 輸出內容 * {"id":"1","name":"zhangsan"} * {"id":"1","name":"zhangsan","age":"22"} * [{"id":"1","name":"zhangsan","age":"22"}] * {"employees":[{"id":"1","name":"zhangsan","age":"22"}]} */
2.將對象轉為JSON
首先創建People類
public class People { private String name; private String idcard; public People() { } public People(String name, String idcard) { this.name = name; this.idcard = idcard; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIdcard() { return idcard; } public void setIdcard(String idcard) { this.idcard = idcard; } }
將對象轉為JSON同樣使用SONObject.fromObject(obj)方法
如果是一個List,轉為JSON時需要使用JSONArray將對象轉為JSON數組
public static void transformObjectTest() { People p1 = new People("a", "111111"); //1.將對象轉為json System.out.println(JSONObject.fromObject(p1)); List<People> peopleList = new ArrayList<People>(); peopleList.add(p1); //2.將list轉為json(需要使用數組JSONArray) JSONArray arr = JSONArray.fromObject(peopleList); System.out.println(arr.toString()); } /*輸出內容 * {"idcard":"111111","name":"a"} * [{"idcard":"111111","name":"a"}] */
3.JSON轉為bean
json轉為bean的方法也非常簡單,只需使用JSONObject.toBean()方法即可,使用該方法的時候需要傳入Bean的class
/** * 將json轉換為bean * @param json * @param type * @return */ public static <T> Object transformJsonToBean(String json, Class<T> type) { JSONObject jsonObject = JSONObject.fromObject(json); return JSONObject.toBean(jsonObject, type); }
4.JSON轉為list<bean>集合
由於是集合,所以需要使用JSONArray,JSONArray提供了toCollection方法,使用該方法同樣需要傳入bean的class
/** * 項目名稱:tools * 項目包名:com.songfayuantools.json * 創建時間:2017年7月31日上午11:58:51 * 創建者:Administrator-宋發元 * 創建地點: */ package com.songfayuantools.json; import com.songfayuantools.entity.UserInfo; import net.sf.json.JSON; import net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; /** * 描述:JSONObject使用方法詳解 * JSONObject-lib包是一個beans,collections,maps,java arrays和xml和JSON互相轉換的包。 * @author songfayuan * 2017年7月31日上午11:58:51 */ public class Json { /** * 描述:json字符串轉java代碼 * @author songfayuan * 2017年8月2日下午2:24:47 */ public static void jsonToJava() { System.out.println("json字符串轉java代碼"); String jsonStr = "{\"password\":\"123456\",\"username\":\"張三\"}"; JSONObject jsonObject = JSONObject.fromObject(jsonStr); String username = jsonObject.getString("username"); String password = jsonObject.getString("password"); System.err.println("json--->java \n username="+username+"\t passwor="+password); } /** * 描述:java代碼封裝為json字符串 * @author songfayuan * 2017年8月2日下午2:30:58 */ public static void javaToJSON() { System.out.println("java代碼封裝為json字符串"); JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "宋發元"); jsonObject.put("age", 24); jsonObject.put("sex", "男"); System.out.println("java--->json \n " + jsonObject.toString()); } /** * 描述:json字符串轉xml字符串 * @author songfayuan * 2017年8月2日下午2:56:30 */ public static void jsonToXML() { System.out.println("json字符串轉xml字符串"); String jsonStr = "{\"username\":\"宋發元\",\"password\":\"123456\",\"age\":\"24\"}"; JSONObject jsonObject = JSONObject.fromObject(jsonStr); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setRootName("user_info"); xmlSerializer.setTypeHintsEnabled(false); String xml = xmlSerializer.write(jsonObject); System.out.println("json--->xml \n" + xml); } /** * 描述:xml字符串轉json字符串 * @author songfayuan * 2017年8月2日下午3:19:25 */ public static void xmlToJSON() { System.out.println("xml字符串轉json字符串"); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>宋發元</username></user_info>"; XMLSerializer xmlSerializer = new XMLSerializer(); JSON json = xmlSerializer.read(xml); System.out.println("xml--->json \n" + json.toString()); } /** * 描述:javaBean轉json字符串 * @author songfayuan * 2017年8月2日下午3:39:10 */ public static void javaBeanToJSON() { System.out.println("javaBean轉json字符串"); UserInfo userInfo = new UserInfo(); userInfo.setUsername("宋發元"); userInfo.setPassword("123456"); JSONObject jsonObject = JSONObject.fromObject(userInfo); System.out.println("JavaBean-->json \n" + jsonObject.toString()); } /** * 描述:javaBean轉xml字符串 * @author songfayuan * 2017年8月2日下午3:48:08 */ public static void javaBeanToXML() { System.out.println("javaBean轉xml字符串"); UserInfo userInfo = new UserInfo(); userInfo.setUsername("songfayuan"); userInfo.setPassword("66666"); JSONObject jsonObject = JSONObject.fromObject(userInfo); XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write(jsonObject, "UTF-8"); System.out.println("javaBean--->xml \n" + xml); } public static void main(String args[]) { // jsonToJava(); // javaToJSON(); // jsonToXML(); // xmlToJSON(); // javaBeanToJSON(); javaBeanToXML(); } }