解决Jackson解析嵌套类问题(MismatchedInputException)
短信预约 -IT技能 免费直播动态提醒
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("class="lazy" data-src/main/resources/doc/windABC.json");
// FileInputStream fip = new FileInputStream(file);
BufferedReader br = new BufferedReader(new FileReader("class="lazy" data-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("class="lazy" data-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:
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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341