JSON转换常用类库
本文主要包括以下几方面:
- jackson的使用
- fastjson的使用
- gson的使用
使用之前,先引入依赖
------------------jackson------------------------------- <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency> ------------------fastjson------------------------------- <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4</version> </dependency> ------------------gson------------------------------- <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
一、jackson的使用
//实体类 public class Users { private String name; private Integer age; private Date birthday; private String email; ... @Test public void testJoinne2r() throws IOException{ //JAVA对象转JSON[JSON序列化] Users user = new Users(); user.setName("小民"); user.setEmail("xiaomin@sina.com"); user.setAge(20); System.out.println(objectMapper.writeValueAsString(user)); //{"name":"小民","age":20,"birthday":null,"email":"xiaomin@sina.com"} //JSON转Java类[JSON反序列化] String jsonstr = objectMapper.writeValueAsString(user); Users u = objectMapper.readValue(jsonstr,Users.class); System.out.println(u.toString()); //Users{name='小民', age=20, birthday=null, email='xiaomin@sina.com'} }
Jackson注解:
@JsonIgnore 用于属性上,表示忽略该属性。
@JsonFormat 用于属性上,把Date类型直接转化为想要的格式,如@JsonFormat(pattern = “yyyy-MM-dd HH-mm-ss”)。
@JsonProperty 用于属性上,相当于重命名,如把trueName属性序列化为name
二、fastjson的使用
//对象序列化 Map<String, Object> map = ImmutableMap.of("key1", "One","key2", "Two"); System.out.println(JSON.toJSONString(map)); //{"key1":"One","key2":"Two"} //传入一个对象和一个布尔类型(是否格式化),将对象转成格式化后的JSON字符串。 JSON.toJSONString(map,true) //反序列化对象 String jsonstr = JSON.toJSONString(map); Map<String, Object> resultMap = JSONObject.parseObject(jsonstr ,Map.class); System.out.println(resultMap);//{key1=One, key2=Two} //日期格式化 String dateJson = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat); System.out.println(dateJson);//"2021-06-29 15:28:55" //集合的反序列化 List<Map<String,Object>> list = Lists.newArrayList(); Map<String, Object> map = ImmutableMap.of("key1", "One","key2", "Two"); list.add(map); String jsonstr = JSON.toJSONString(list,true); List<Map<String,Object>> result = JSONArray.parseObject(jsonstr ,List.class); System.out.println(result);//[{"key1":"One","key2":"Two"}]
自定义一个fastjson转换工具类
public class MyJsonUtil { private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段 SerializerFeature.WriteNullListAsEmpty, // // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullBooleanAsFalse, // // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullStringAsEmpty, // // 字符类型字段如果为null,输出为"",而不是null SerializerFeature.WriteDateUseDateFormat // 日期格式化yyyy-MM-dd // HH:mm:ss }; public static String toJson(Object object) { return JSON.toJSONString(object, features); } }
三、gson的使用
public class User { //省略其它 public String name; public int age; public String emailAddress; } //生成JSON: Gson gson = new Gson(); User user = new User("张三",24); String jsonObject = gson.toJson(user); // {"name":"张三kidou","age":24} //解析JSON: Gson gson = new Gson(); String jsonString = "{\"name\":\"张三\",\"age\":24}"; User user = gson.fromJson(jsonString, User.class); //属性重命名 @SerializedName 注解的使用 @SerializedName("email_address") public String emailAddress;