最近常用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对象