最近常用json的一些轉換,使用的是fastjson,因此就對fastjson進行了一些彙總,記錄下來。java
首先是一些類庫的說明:json
SerializeWriter:至關於StringBufferapi
JSONArray:至關於List<Object>數組
JSONObject:至關於Map<String, Object>spa
JSON反序列化沒有真正數組,本質類型都是List<Object>code
關於JSON類的一些api:orm
public static final Object parse(String text); // 把JSON文本parse爲JSONObject或者JSONArray public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse爲JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 將JavaBean序列化爲JSON文本 public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化爲帶格式的JSON文本 public static final Object toJSON(Object javaObject); 將JavaBean轉換爲JSONObject或者JSONArray。
JSONObject jsonObject = new JSONObject(); //建立一個json對象 jsonObject.put("hello","hello"); jsonObject.put("world","world"); jsonObject.put("json","json"); String jsonString = jsonObject.toJSONString(); //json轉換成String //打印結果以下:{"world":"world","json":"json","hello":"hello"} //須要注意的是:put是在前面put進去。 String json = "{\"world\":\"world\",\"json\":\"json\",\"hello\":\"hello\"}"; //這是一個json字符串 JSONObject jsonObject = JSONObject.parseObject(json); //轉換成json對象 System.out.println(jsonObject.toJSONString()); //{"world":"world","json":"json","hello":"hello"} //添加一個新的字段到json對象中並打印 jsonObject.put("content","這是新添加進入的一個字段"); System.out.println(jsonObject.toJSONString()); //{"world":"world","json":"json","hello":"hello","content":"這是新添加進入的一個字段"}
User user = new User(); user.setPassword("111"); user.setUsername("222"); user.setCreateDate(new Date()); String jsonString = JSONObject.toJSONString(user); //將javaBean序列化成json對象(javaBean中的date類型數據不須要格式化) System.out.println(jsonString); //{"createDate":1532495262966,"password":"111","username":"222"} //若是時間須要格式化,能夠使用下面的 String s = JSONObject.toJSONStringWithDateFormat(user, "yyyy-MM-dd"); System.out.println(s); //{"createDate":"2018-07-25","password":"111","username":"222"} //目前網上查到的能夠識別的時間格式是:yyyy-MM-dd,yyyy-MM-dd HH:mm:ss,yyyy-MM-dd HH:mm:ss,毫秒數字,毫秒數字字符串,new Date(198293238)類型 //將對象字符串轉換成javaBean User parseUser = JSONObject.parseObject(s, User.class); System.out.println(parseUser.toString()); //User{username='222', password='111', createDate=Wed Jul 25 00:00:00 CST 2018}
Map<String,Object> map = new HashMap<>(); map.put("hello","hello"); map.put("world","world"); map.put("json","json"); String jsonString = JSON.toJSONString(map); String jsonStringPettyFormat = JSON.toJSONString(map,true); String jsonString1 = JSON.toJSONString(map); System.out.println(jsonString); //{"world":"world","json":"json","hello":"hello"} System.out.println(jsonStringPettyFormat); //帶格式的json { "world":"world", "json":"json", "hello":"hello" } System.out.println(jsonString1); //{"world":"world","json":"json","hello":"hello"} //將json轉換成map Map map1 = JSON.parseObject(jsonString, Map.class); //{world=world, json=json, hello=hello} Map map2 = JSON.parseObject(jsonStringPettyFormat, Map.class); //{world=world, json=json, hello=hello} Map map3 = JSON.parseObject(jsonString1, Map.class); //{world=world, json=json, hello=hello}
List<User> userList = new ArrayList<>(); userList.add(new User("111","222",new Date())); userList.add(new User("222","222",new Date())); String jsonString = JSON.toJSONStringWithDateFormat(userList,"yyyy-MM-dd"); //[{"createDate":"2018-07-25","password":"222","username":"111"},{"createDate":"2018-07-25","password":"222","username":"222"}] //將json轉換成list List<User> userList1 = JSON.parseArray(jsonString, User.class); //[User{username='111', password='222', createDate=Wed Jul 25 16:49:11 CST 2018}, User{username='222', password='222', createDate=Wed Jul 25 16:49:11 CST 2018}] List<Map> maps = JSON.parseArray(jsonString, Map.class); //[{password=222, createDate=2018-07-25, username=111}, {password=222, createDate=2018-07-25, username=222}]
特別的:能夠將map轉換成json而後轉換成javaBean對象