解決Jackson解析嵌套類問題(MismatchedInputException)
Jackson解析嵌套類(MismatchedInputException)
具體報錯如下
問題描述:Jackson解析嵌套類問題
調(diào)用接口返回json格式的數(shù)據(jù),使用Jackson解析參數(shù)轉(zhuǎn)換成對象:
@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; } } } } }
問題本質(zhì)為:內(nèi)部非靜態(tài)類無法實例化
你需要做兩件事:
- 給內(nèi)部類前面加上static
- 給內(nèi)部類加上默認(rèn)構(gòu)造函數(shù)
改過后的內(nèi)部類像這樣:
@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數(shù)據(jù)
數(shù)據(jù) ------》打印每條數(shù)據(jù)
{ "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的節(jié)點 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);//遞歸調(diào)用,判斷是否數(shù)組嵌套數(shù)組 } bufferedWriter.newLine(); }else { bufferedWriter.write(jsonNode.asText()); bufferedWriter.newLine(); } } }
輸出結(jié)果
項目:******** 部門:******** 工號:********
ID:
10001detail:
detailjson_format_version:
1.0other_info:
array_one:
855
410854
411847
411846
410845
410844
409
array_two:
832
303829
303828
302825
302824
301
array_three:
1013
2241012
2251010
2251009
2261023
224
point:
853
310boolean:
true
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud分布式鏈路追蹤組件Sleuth配置詳解
這篇文章主要介紹了SpringCloud鏈路追蹤組件Sleuth配置方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-11-11java實現(xiàn)日歷應(yīng)用程序設(shè)計
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)日歷應(yīng)用程序設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06springboot+thymeleaf國際化之LocaleResolver接口的示例
本篇文章主要介紹了springboot+thymeleaf國際化之LocaleResolver的示例 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Mybatis 如何批量刪除數(shù)據(jù)的實現(xiàn)示例
這篇文章主要介紹了Mybatis 如何批量刪除數(shù)據(jù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03