• jack反序列化自定义字段绑定,报错:can only instantiate non-static inner class by using default, no-argument constructor


    package com.xxx;
    
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.List;
    
    public class TestJsonNode {
    
        @Test
        public void test()
        {
            String json = "{"status": 0, "segment": 0, "result": {"hypotheses": [{"transcript": "原 的 "}], "final": false}, "id": "ba040ef0-9134-46f3-8a95-cc9b70868c07"}";
            ObjectMapper objectMapper = new ObjectMapper();
            try {
    //            JsonNode node = objectMapper.readTree(json);
    //            boolean containsStatusField = node.has("status");
    //            if(!containsStatusField)
    //            {
    //                System.out.println("服务器异常");
    //            }
    //
    //            int status = node.findValue("status").intValue();
                //System.out.println(status);
    
                ASRConversionResponse response = objectMapper.readValue(json, ASRConversionResponse.class);
    
                System.out.println(response);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    @Data
    @NoArgsConstructor
    public class ASRConversionResponse
    {
        private Integer status;
        private String message;
    
        private List<Result> result;
    
        @JsonProperty("segment_start") //此处为自定义字段绑定类型
        private Float segmentStart;
    
        @JsonProperty("segment_length")
        private Float segmentLength;
    
        @JsonProperty("total_length")
        private Float totalLength;
    
        private short segment;
    
        private String id;
    
    }
    
    @Data
    @NoArgsConstructor
      class Result{
    
        @JsonProperty("final")
        private Boolean _final;
    
        private List<Hypotheses> hypotheses;
    }
    
    @Data
    @NoArgsConstructor
      class Hypotheses
    {
        private Float likelihood;
        private String transcript;
    }
    
    }

    出错原因:把ASRConversionResponse内部类提出来,单独新建一个类ASRConversionResponse.java

  • 相关阅读:
    「自己开发直播」实现nginx-rtmp-module多频道输入输出与权限控制
    抢购代码留存
    抢红包代码留存
    Table '' is marked as crashed and should be repaired 解决方法
    extundelete实现Linux下文件/文件夹数据恢复!
    RedHat设置Yum源
    MFC 自定义消息
    单例模式
    工厂模式(转)
    hash_map
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11316538.html
Copyright © 2020-2023  润新知