使用objectmapper将json数据转换为java类时发生不匹配InputException

jdg4fx2g  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(268)

我想使用jackson将这样的json数据加载到我的java程序中:

  1. [
  2. {
  3. "id":1246524522,
  4. "version":50,
  5. "createdOn":"2020-09-30T12:22:53.785+0000",
  6. "createdBy":"me",
  7. "lastModifiedOn":"2020-09-30T12:22:53.785+0000",
  8. "name":"Unused Template",
  9. "description":"Template"
  10. },
  11. {
  12. "id":10739765484,
  13. "version":70,
  14. "createdOn":"2020-12-21T11:39:51.941+0000",
  15. "createdBy":"creator",
  16. "lastModifiedOn":"2020-12-21T11:39:51.941+0000",
  17. "name":"TestTag202006",
  18. "description":""
  19. }
  20. ]

我为此编写了以下课程:

  1. public class TagJSON {
  2. @JsonProperty
  3. private Long id;
  4. @JsonProperty
  5. private Long version;
  6. @JsonProperty
  7. private String createdOn;
  8. @JsonProperty
  9. private String createdBy;
  10. @JsonProperty
  11. private String lastModifiedOn;
  12. @JsonProperty
  13. private String name;
  14. @JsonProperty
  15. private String description;
  16. public TagJSON() {
  17. }
  18. public TagJSON(Long id, Long version, String createdOn, String createdBy, String lastModifiedOn, String name, String description) {
  19. this.id = id;
  20. this.version = version;
  21. this.createdOn = createdOn;
  22. this.createdBy = createdBy;
  23. this.lastModifiedOn = lastModifiedOn;
  24. this.name = name;
  25. this.description = description;
  26. }
  27. public Long getId() {
  28. return id;
  29. }
  30. public void setId(Long id) {
  31. this.id = id;
  32. }
  33. public Long getVersion() {
  34. return version;
  35. }
  36. public void setVersion(Long version) {
  37. this.version = version;
  38. }
  39. public String getCreatedOn() {
  40. return createdOn;
  41. }
  42. public void setCreatedOn(String createdOn) {
  43. this.createdOn = createdOn;
  44. }
  45. public String getCreatedBy() {
  46. return createdBy;
  47. }
  48. public void setCreatedBy(String createdBy) {
  49. this.createdBy = createdBy;
  50. }
  51. public String getLastModifiedOn() {
  52. return lastModifiedOn;
  53. }
  54. public void setLastModifiedOn(String lastModifiedOn) {
  55. this.lastModifiedOn = lastModifiedOn;
  56. }
  57. public String getName() {
  58. return name;
  59. }
  60. public void setName(String name) {
  61. this.name = name;
  62. }
  63. public String getDescription() {
  64. return description;
  65. }
  66. public void setDescription(String description) {
  67. this.description = description;
  68. }
  69. @Override
  70. public String toString() {
  71. return "TagJSON{" +
  72. "id=" + id +
  73. ", version=" + version +
  74. ", createdOn='" + createdOn + '\'' +
  75. ", createdBy='" + createdBy + '\'' +
  76. ", lastModifiedOn='" + lastModifiedOn + '\'' +
  77. ", name='" + name + '\'' +
  78. ", description='" + description + '\'' +
  79. '}';
  80. }
  81. }
  82. public class AllTagsJson {
  83. private List<TagJSON> allTags;
  84. public AllTagsJson(List<TagJSON> allTags) {
  85. this.allTags = allTags;
  86. }
  87. public List<TagJSON> getAllTags() {
  88. return allTags;
  89. }
  90. public void setAllTags(List<TagJSON> allTags) {
  91. this.allTags = allTags;
  92. }
  93. @Override
  94. public String toString() {
  95. return "AllTagsJson{" +
  96. "allTags=" + allTags +
  97. '}';
  98. }
  99. }

我想这样运行:

  1. private AllTagsJson doJSONStuff() throws IOException {
  2. ObjectMapper objectMapper = new ObjectMapper();
  3. // objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
  4. // objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
  5. // objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
  6. // AllTagsJson tagJSON = objectMapper.readValue(new File("tag_json.json"), AllTagsJson.class);
  7. List<TagJSON> tags = Arrays.asList(objectMapper.readValue(new File("tag_json.json"), TagJSON[].class));
  8. AllTagsJson allTags = new AllTagsJson(tags);
  9. return allTags;
  10. }

我已经试过了
我得到以下错误:
原因:com.fasterxml.jackson.databind.exc.missmatchdinputException:无法构造的示例 com.example.demo.TagJSON (尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法从[source:(file)处的字符串值('com.example.demo.tagjson')反序列化;行:1,列:1]
错误怎么可能发生在第1行第1列?在我砸坏电脑之前请帮忙:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题