解决Jackson解析嵌套类问题(MismatchedInputException)

这篇文章主要介绍了解决Jackson解析嵌套类问题(MismatchedInputException),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Jackson解析嵌套类(MismatchedInputException)

具体报错如下

问题描述:Jackson解析嵌套类问题 

调用接口返回json格式的数据,使用Jackson解析参数转换成对象:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PixlIdDto implements Serializable {
 
    @JsonIgnore
    private static final long serialVersionUID = -5776690969534186379L;
 
    @JsonProperty("models")
    private List<Models> models;
 
    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Models implements Serializable {
 
        @JsonIgnore
        private static final long serialVersionUID = 9189184337502771734L;
 
        @JsonProperty("medias")
        private List<Medias> medias;
 
        @JsonProperty("code")
        private String code;
        @Data
        @JsonIgnoreProperties(ignoreUnknown = true)
        public class Medias implements Serializable {
 
            @JsonIgnore
            private static final long serialVersionUID = 136346277159168673L;
 
            @JsonProperty("mediaZones")
            private List<MediaZones> mediaZones;
            @Data
            @JsonIgnoreProperties(ignoreUnknown = true)
            public class MediaZones implements Serializable {
 
                @JsonIgnore
                private static final long serialVersionUID = 7683892920280290206L;
 
                @JsonProperty("medias")
                private List<MediasInner> medias;
 
                @JsonProperty("name")
                private String name;
                @Data
                @JsonIgnoreProperties(ignoreUnknown = true)
                public class MediasInner implements Serializable {
 
                    @JsonIgnore
                    private static final long serialVersionUID = 8653771742539974404L;
 
                    @JsonProperty("displayOrder")
                    private int displayOrder ;
 
                    @JsonProperty("pixlId")
                    private String pixlId;
                }
            }
        }
    }
}

问题本质为:内部非静态类无法实例化

你需要做两件事:

  • 给内部类前面加上static
  • 给内部类加上默认构造函数

改过后的内部类像这样:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PixlIdDto implements Serializable {
 
    @JsonIgnore
    private static final long serialVersionUID = -5776690969534186379L;
 
    @JsonProperty("models")
    private List<Models> models;
 
    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    @NoArgsConstructor
    public static class Models implements Serializable {
 
        @JsonIgnore
        private static final long serialVersionUID = 9189184337502771734L;
 
        @JsonProperty("medias")
        private List<Medias> medias;
 
        @JsonProperty("code")
        private String code;
 
        @Data
        @JsonIgnoreProperties(ignoreUnknown = true)
        @NoArgsConstructor
        public static class Medias implements Serializable {
 
            @JsonIgnore
            private static final long serialVersionUID = 136346277159168673L;
 
            @JsonProperty("mediaZones")
            private List<MediaZones> mediaZones;
 
            @Data
            @JsonIgnoreProperties(ignoreUnknown = true)
            @NoArgsConstructor
            public static class MediaZones implements Serializable {
 
                @JsonIgnore
                private static final long serialVersionUID = 7683892920280290206L;
 
                @JsonProperty("medias")
                private List<MediasInner> medias;
 
                @JsonProperty("name")
                private String name;
 
                @Data
                @JsonIgnoreProperties(ignoreUnknown = true)
                @NoArgsConstructor
                public static class MediasInner implements Serializable {
 
                    @JsonIgnore
                    private static final long serialVersionUID = 8653771742539974404L;
 
                    @JsonProperty("displayOrder")
                    private int displayOrder;
 
                    @JsonProperty("pixlId")
                    private String pixlId;
                }
            }
        }
    }
}

问题完美解决。

Jackson处理嵌套Json数据

数据 ------》打印每条数据

{
  "ID": 10001,
  "detail": "detail",
  "json_format_version": 1.0,
  "other_info": {
    "array_one": [
      [855, 410],
      [854, 411],
      [847, 411],
      [846, 410],
      [845, 410],
      [844, 409]
    ],
    "array_two": [
      [832, 303],
      [829, 303],
      [828, 302],
      [825, 302],
      [824, 301]
    ],
    "array_three": [
      [1013, 224],
      [1012, 225],
      [1010, 225],
      [1009, 226],
      [1023, 224]
    ],
    "point": [853, 310],
    "boolean": true
  }
}

代码

public class Test {
    public static void main(String[] args) throws IOException {
//        File file = new File("src/main/resources/doc/windABC.json");
//        FileInputStream fip = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new FileReader("src/main/resources/doc/resource.json"));
        ObjectMapper mapper = new ObjectMapper();
        //读取json的节点
        JsonNode node = mapper.readTree(br);
        //获取所有的key
        Iterator<String> keys = node.fieldNames();

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/main/resources/doc/result.txt"));
        bufferedWriter.write("项目:**********\t\t");
        bufferedWriter.write("部门:**********\t\t");
        bufferedWriter.write("工号:**********\n");

        while (keys.hasNext()) {
            String key = keys.next();
            bufferedWriter.write(key + ":");
            bufferedWriter.newLine();
            if (key.equals("other_info")) {
                JsonNode other_infoNode = mapper.readTree(node.get(key).toString());
                Iterator<String> other_info_Key = other_infoNode.fieldNames();
                for (JsonNode jsonNode : other_infoNode) {
                    bufferedWriter.write(other_info_Key.next().toString()  + ":\n");
                    isArrayNode(jsonNode,mapper,bufferedWriter);
                }
            } else {
                bufferedWriter.write(node.get(key).asText());
                bufferedWriter.newLine();
            }
            bufferedWriter.newLine();

        }
        bufferedWriter.close();
        br.close();
    }

    public static void isArrayNode(JsonNode jsonNode,ObjectMapper mapper,BufferedWriter bufferedWriter) throws IOException {
        if (jsonNode.isArray()) {
            for (JsonNode node1 : jsonNode) {
                isArrayNode(node1,mapper,bufferedWriter);//递归调用,判断是否数组嵌套数组
            }
            bufferedWriter.newLine();
        }else {
           bufferedWriter.write(jsonNode.asText());
           bufferedWriter.newLine();
        }
    }
}

输出结果

项目:********        部门:********        工号:********
ID:
10001

detail:
detail

json_format_version:
1.0

other_info:
array_one:
855
410

854
411

847
411

846
410

845
410

844
409


array_two:
832
303

829
303

828
302

825
302

824
301


array_three:
1013
224

1012
225

1010
225

1009
226

1023
224


point:
853
310

boolean:
true

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。

本文标题为:解决Jackson解析嵌套类问题(MismatchedInputException)

基础教程推荐