maven依賴:java
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.22</version> </dependency>
舉例:json
import java.util.ArrayList; import java.util.Date; import java.util.List; import com.alibaba.fastjson.JSON; /** * 若是對象的屬性沒有被賦值,那麼將對象轉換爲json串時: * * 字符串類型的屬性: 默認會忽略該屬性 * Integer類型的屬性: 默認會忽略該屬性 * Date類型的屬性: 默認會忽略該屬性 * int類型的屬性: 默認會被轉換爲0 */ public class TestFastJson { public static void main(String[] args) { User obj1 = new User(); obj1.setAge(1); obj1.setName("jack"); User obj2 = new User(); obj2.setAge(2); obj2.setName("tom"); obj2.setTime(new Date()); ArrayList<User> arrayObj = new ArrayList<User>(); arrayObj.add(obj1); arrayObj.add(obj2); // 將對象轉換爲json串 String jsonString1 = JSON.toJSONString(obj1); // {"age":1,"name":"jack"} String jsonString2 = JSON.toJSONString(arrayObj); // [{"age":1,"name":"jack"},{"age":2,"name":"tom","time":1503289848086}] // 將json串轉換爲對象類型 User user = JSON.parseObject(jsonString1, User.class); List<User> userList = JSON.parseArray(jsonString2, User.class); } }
maven依賴:數組
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency>
舉例:maven
import java.util.ArrayList; import java.util.Date; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; /** * 若是對象的屬性沒有被賦值,那麼將對象轉換爲json串時: * * 字符串類型的屬性: 默認會被轉換爲空串"" * int、Integer類型的屬性: 默認會被轉換爲0 * Date類型的屬性: 默認會被轉換爲null * */ public class TestSfJson { public static void main(String[] args) { // 1.普通對象轉換爲json串 User obj1 = new User(); obj1.setName("jack"); obj1.setAge(17); JSONObject jsonObj1 = JSONObject.fromObject(obj1); String jsonStr1 = jsonObj1.toString(); System.out.println(jsonStr1); // {"age":17,"name":"jack"} // 2.設置Date類型屬性值的處理策略 Employee obj2 = new Employee(); obj2.setName("jack"); obj2.setAge(17); obj2.setBirthday(new Date()); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); JSONObject jsonObj2 = JSONObject.fromObject(obj2,jsonConfig); String jsonStr2 = jsonObj2.toString(); System.out.println(jsonStr2); // {"age":17,"birthday":"2017-08-21 11:04:47","name":"jack"} // 3.數組對象轉換爲json串 User user1 = new User(); user1.setName("jack"); user1.setAge(17); User user2 = new User(); user2.setName("tom"); user2.setAge(18); List<User> obj3 = new ArrayList<User>(); obj3.add(user1); obj3.add(user2); // 或:User[] obj3 = {user1,user2}; JSONArray jsonObj3 = JSONArray.fromObject(obj3); String jsonStr3 = jsonObj3.toString(); System.out.println(jsonStr3); // [{"age":17,"name":"jack"},{"age":18,"name":"tom"}] // 4.直接構造JSONObject對象 JSONObject jsonObj = new JSONObject(); jsonObj.put("name", "jack"); jsonObj.put("age", 17); String jsonStr = jsonObj.toString(); System.out.println(jsonStr); // {"name":"jack","age":17} } } import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor { private String format = "yyyy-MM-dd HH:mm:ss"; public JsonDateValueProcessor() { super(); } public JsonDateValueProcessor(String format) { super(); this.format = format; } [@Override](https://my.oschina.net/u/1162528) public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) { return process(paramObject); } [@Override](https://my.oschina.net/u/1162528) public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) { return process(paramObject); } private Object process(Object value) { if (value instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA); return sdf.format(value); } return value == null ? "" : value.toString(); } }