0

您好我正在尝试将 JSON 数据转换为类,我使用这样的反序列化方法:

public class GroupDeserlialize extends StdDeserializer<Employe> {
public GroupDeserlialize()
{
  this(null);
}
public GroupDeserlialize(Class<?> vc)
{
  super(vc);
}
@Override
public Employe deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  // TODO Auto-generated method stub

      JsonNode node = p.getCodec().readTree(p);
      int matricule = (Integer) node.get("matricule").intValue();
      String nom= (String) node.get("nom").textValue();
      String prenom= (String) node.get("prenom").textValue();
      String nom_ar= (String) node.get("nom_ar").textValue();
      String prenom_ar= (String) node.get("prenom_ar").textValue();
      int age = (Integer) node.get("age").numberValue();
      String description= (String) node.get("description").textValue();
      String email= (String) node.get("email").textValue();
      String adresse= (String) node.get("adresse").textValue();
      String ville= (String) node.get("ville").textValue();
      int codeP = (Integer) node.get("codeP").intValue();
      int numTele = (Integer) node.get("numTele").intValue();
     // Long idGrp = (Long) ((LongNode) node.get("idGrp")).numberValue();
   
    
      Long idGrp = (Long) node.get(0).numberValue();
       String nomGroup =  (String)  node.get("nomGroup").asText();   
return new Employe( matricule,  nom,  prenom,  nom_ar,  prenom_ar,  age, description,  email,  codeP,  numTele,  adresse,  ville, new Group(idGrp,nomGroup));
}}

我要发送的 JSON(如果 JSON 格式错误,这就是 Spring Boot IDK 的角度返回/n):

{
  "matricule" : "125",
  "nom" : "Assaibi",
  "prenom" : "Takwa",
  "nom_ar" : "Assaibi",
  "prenom_ar" : "Takwa",
  "age" : 34,
  "description" : "kjjhhdh",
  "codeP" : "2087",
  "email" : "takwaassaibi08@gmail.com",
  "numTele" : "51890562",
  "adresse" : "4270 citè folla agba Tunis",
  "ville" : "Tunis",
  "group" : "{\n  \"nomGroup\": \"groupeMembre\",\n  \"idGrp\": 3\n}"
}

这是包含一个组的类雇员

@Entity
@JsonDeserialize(using = GroupDeserlialize.class)
public class Employe implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "myid")
   @GenericGenerator(name = "myid", strategy = "entity.MyGenerator")
   private Integer matricule;

   private String   nom;
   private String prenom;
   private String nom_ar;
   private String prenom_ar;
   private int age;
   private String description;
   private String email;
   private int codeP;
   private int numTele;
   private String adresse;
   private String  ville;
   
   @JoinColumn(name="idGrp")
   @ManyToOne(cascade = {CascadeType.ALL})  
   @Embedded
   Group group;
   
    public Employe(Integer matricule, String nom, String prenom, String nom_ar, String prenom_ar, int age,
        String description, String email, int codeP, int numTele, String adresse, String ville, Group group) {
      super();
      this.matricule = matricule;
      this.nom = nom;
      this.prenom = prenom;
      this.nom_ar = nom_ar;
      this.prenom_ar = prenom_ar;
      this.age = age;
      this.description = description;
      this.email = email;
      this.codeP = codeP;
      this.numTele = numTele;
      this.adresse = adresse;
      this.ville = ville;
    }
}

这是 Group 类

@Entity
@Table(name="groups")
public class Group implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long idGrp;
    
    private String NomGroup;
    
    public Group(){
    }

    public Group(Long id, String nomGroup) {
        super();
        this.idGrp = id;
        NomGroup = nomGroup;
    }
}

和主班

public static void main(String[] args) throws  JsonProcessingException {
    SpringApplication.run(CniAppApplication.class, args);
    Employe s=new Employe();
    ObjectMapper mapper = new ObjectMapper();
    /*SimpleModule module = new SimpleModule();
    module.addDeserializer(Employe.class, new GroupDeserlialize());
    mapper.registerModule(module);

    try {
      Employe readValue = mapper.readValue(mapper.writeValueAsString(s), Employe.class);
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }*/
    Employe itemWithOwner = new ObjectMapper().readValue(mapper.writeValueAsString(s), Employe.class);
  }
}

我得到的例外:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.numberValue()" because the return value of "com.fasterxml.jackson.databind.JsonNode.get(int)" is null
    at entity.GroupDeserlialize.deserialize(GroupDeserlialize.java:48)
    at entity.GroupDeserlialize.deserialize(GroupDeserlialize.java:1)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
    at cni.tn.CniApp.CniAppApplication.main(CniAppApplication.java:57)
4

1 回答 1

0

我认为该问题是由以下行引起的:

Long idGrp = (Long) node.get(0).numberValue();

group在您的 JSON 中是另一个String. 它恰好是 JSON,但据您Deserializer所知,它是常规的String. 这意味着您需要使用ObjectMapperJSON 来读取它。您可以按如下方式执行此操作:

public class GroupDeserlialize extends StdDeserializer<Employe> {

    private ObjectMapper objectMapper;

    public GroupDeserlialize(ObjectMapper objectMapper) {
        this(objectMapper, null);
    }

    public GroupDeserlialize(ObjectMapper objectMapper, Class<?> vc) {
        super(vc);
        this.objectMapper = objectMapper;
    }

    @Override
    public Employe deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        int matricule = node.get("matricule").intValue();
        String nom= node.get("nom").textValue();
        String prenom= node.get("prenom").textValue();
        String nom_ar= node.get("nom_ar").textValue();
        String prenom_ar= node.get("prenom_ar").textValue();
        int age = (Integer) node.get("age").numberValue();
        String description= node.get("description").textValue();
        String email= node.get("email").textValue();
        String adresse=  node.get("adresse").textValue();
        String ville= node.get("ville").textValue();
        int codeP = node.get("codeP").intValue();
        int numTele = node.get("numTele").intValue();

        String groupJson = node.get("group").textValue();
        Group group = objectMapper.readValue(groupJson, Group.class);

        return new Employe(matricule, nom, prenom, nom_ar, prenom_ar, age, description, email, codeP, numTele, adresse, ville, group);
    }
}

这也意味着您需要更改主类:

public static void main(String[] args) throws  JsonProcessingException {
    SpringApplication.run(CniAppApplication.class, args);
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Employe.class, new GroupDeserlialize(mapper));
    mapper.registerModule(module);

    try {
      Employe readValue = mapper.readValue(new File("input.json"), Employe.class);
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

这假设您将 JSON 文件放在一个input.json文件中。这显然仅用于测试目的。

您还需要将设置器添加到Group类:

public class Group {

    private Long idGrp;

    private String nomGroup;

    public Group(){
    }

    public Group(Long id, String nomGroup) {
        super();
        this.idGrp = id;
        this.nomGroup = nomGroup;
    }

    public void setIdGrp(Long idGrp) {
        this.idGrp = idGrp;
    }

    public void setNomGroup(String nomGroup) {
        this.nomGroup = nomGroup;
    }
}
于 2021-09-27T23:04:36.333 回答