首頁 > 軟體

解決Jackson解析巢狀類問題(MismatchedInputException)

2022-06-07 14:07:41

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("專案:**********tt");
        bufferedWriter.write("部門:**********tt");
        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

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com