// pom依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
ジャクソンのreadValueの使用と書き込み
1.定义一个User对象
public class User {
private String name;
private int age;
private Date birthday; // 没有使用jackson注解将会变成时间戳
private String email;
public User(String name, int age, Date birthday, String email) {
this.name = name;
this.age = age;
this.birthday = birthday;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2.使用jackson 的ObjectMapper 类中的 readValue()方法进行转换
public class JsonTest
{
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
User user=new User("隔壁老王", 18, new Date(), "110");
User user1=new User("隔壁老王", 18, new Date(), "110");
List<User> list = new ArrayList<User>();
list.add(user);
list.add(user1);
//转换器
ObjectMapper mapper = new ObjectMapper();
// 对象--json数据
String json=mapper.writeValueAsString(user); //将对象转换成json
// 将json 字符串转成User类
User u = mapper.readValue(json, User.class);
// 将json 字符串转化成List<User>
List<User> list1 = mapper.readValue(list, new TypeReference<List<People>>(){}f)
// 其他的如将文件中转化成类的方法也相识 如
// readValue(File src, TypeReference valueTypeRef)
// readValue(File src, Class<T> valueType)
}
}
3.使用jackson 的ObjectMapper 类中的 writerValue()方法进行转换
public class JsonTest
{
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
User user=new User("隔壁老王", 18, dateformat.parse("1996-10-01"), "110");
//转换器
ObjectMapper mapper = new ObjectMapper();
//User类转JSON 字符串
String json = mapper.writeValueAsString(user);
System.out.println(json);
//Java集合转JSON 字符串
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);
}
}
}
4.使用jackson ObjectMapper获取具体json 字符串中某个key值对于的value
// 假设 json字符串格式为 {content:{rendered: "23"}, titile:{rendered: "别跑"}} 则获取方式如下:
ObjectMapper mapper = new ObjectMapper();
mapper.readTree(content).forEach(jsonNode -> {
String renderedContent = jsonNode.path("content").path("rendered").asText("");
String title = jsonNode.path("title").path("rendered").asText("");
});
5.使用jackson 的提供了一系列注解
@JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。
@JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。
@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化
リンクします。https://www.jianshu.com/p/7e4bc9a535c8
ジャクソンノートと一般的な使用法
最近のプロジェクトは、覚えておくことはあなたを助けるために、合計する、ジャクソンのノートの一部を使用し、書きました。
JsonIgnoreと@JsonIgnoreProperties @ 1
2つの注釈の後、対照と比較して使用することもできます。
@JsonIgnore
いくつかのプロパティでJSONシリアライズのJava Beanにすると無視は、シリアライズとデシリアライズが影響を受けます。
プライベート文字列名;
プライベート整数歳。
@JsonIgnore
プライベート文字列の色。
コンソールの方法でプリントラン
System.out.println(名前+ "" +年齢+ "" +カラー+ "");
色だけが影響を受けたノートが表示されないが、名前と年齢を表示します。
メソッドを使用してください:一般的なマーキングプロパティまたはメソッド、JSONは、プロパティが含まれていない、すなわち、データを返さに。
@JsonIgnoreProperties
そして、ジャクソンは無視することを告げている属性と同じ@JsonIgnoreの役割、
@JsonIgnoreProperties除いてあるクラスレベルと同時に、複数の属性を指定することもできます。
@JsonIgnoreProperties(値= { "年齢"、 "色"})
パブリッククラスTestJackson {
プライベート文字列ID;
プライベート文字列のユーザ名。
プライベート文字列のパスワード。
プライベート整数歳。
プライベート文字列の色。
}
クラス年齢と色の属性がシリアライズおよびデシリアライズ時に無視されます。
2. @ JsonProperty
それは、財産上の役割で使用される他に、属性の名前シーケンスの名前を
@JsonProperty( "名前")
プライベート文字列のユーザ名。
ユーザ名属性は、名前としてシリアル化されます
JsonInclude @ 3.(JsonInclude.Include.NON_NULL)
この注釈が示している場合、値は、nullを返されませんされて注釈をクラスに追加することができる、相互変換、エンティティクラスのJSON 属性値がnullではシリアライズに参加しません。
@JsonInclude(JsonInclude.Include.NON_NULL)
公共のベクトルの子供たち。
@JsonInclude(JsonInclude.Include.NON_NULL)
パブリッククラスTestJackson {
XXXXXX