Maison  >  Article  >  Java  >  Comment définir des noms alternatifs pour les champs en Java à l'aide de Jackson ?

Comment définir des noms alternatifs pour les champs en Java à l'aide de Jackson ?

王林
王林avant
2023-08-27 13:49:05773parcourir

Comment définir des noms alternatifs pour les champs en Java à laide de Jackson ?

L'annotation @JsonAlias peut définir un ou plusieurs noms alternatifs pour les attributs acceptés lors de la désérialisation, définissant les données JSON sur un objet Java. Mais lors de la sérialisation, c'est-à-dire lors de l'obtention du JSON à partir d'un objet Java, seul le nom réel de la propriété logique est utilisé à la place du alias.

Syntaxe

<strong>@Target(value={ANNOTATION_TYPE,FIELD,METHOD,PARAMETER})
@Retention(value=RUNTIME)
public @interface JsonAlias</strong>

Exemple

的中文翻译为:

示例

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class ObjectToJsonTest {
   public static void main(String[] args) throws JsonProcessingException {
      <strong>ObjectMapper </strong>mapper = new <strong>ObjectMapper()</strong>;
      Technology tech = new Technology("Java", "Oracle");
      Employee emp = new Employee(110, "Raja", tech);
      String jsonWriter = mapper.<strong>writerWithDefaultPrettyPrinter().writeValueAsString(emp);</strong>
      System.out.println(jsonWriter);
   }
}
<strong>// Technology class
</strong>class Technology {
<strong>   @JsonProperty("skill")
</strong>   private String skill;
<strong>   @JsonProperty("subSkill")
</strong><strong>   @JsonAlias({"sSkill", "mySubSkill"})
</strong>   private String subSkill;
   public Technology(){}
   public Technology(String skill, String subSkill) {
      this.skill = skill;
      this.subSkill = subSkill;
   }
   public String getSkill() {
      return skill;
   }
   public void setSkill(String skill) {
      this.skill = skill;
   }
   public String getSubSkill() {
      return subSkill;
   }
   public void setSubSkill(String subSkill) {
      this.subSkill = subSkill;
   }
}
<strong>// Employee class
</strong>class Employee {
<strong>   @JsonProperty("empId")
</strong>   private Integer id;
<strong>   @JsonProperty("empName")
</strong><strong>   @JsonAlias({"ename", "myename"})
</strong>   private String name;
<strong>   @JsonProperty("empTechnology")
</strong>   private Technology tech;
   public Employee(){}
   public Employee(Integer id, String name, Technology tech){
      this.id = id;
      this.name = name;
      this.tech = tech;
   }
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Technology getTechnology() {
      return tech;
   }
   public void setTechnology(Technology tech) {
      this.tech = tech;
   }
}

输出

<strong>{
 "technology" : {
 "skill" : "Java",
 "subSkill" : "Oracle"
 },
 "empId" : 110,
 "empName" : "Raja",
 "empTechnology" : {
 "skill" : "Java",
 "subSkill" : "Oracle"
 }
}</strong>

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer