我想使用jackson将这样的json数据加载到我的java程序中:
[
{
"id":1246524522,
"version":50,
"createdOn":"2020-09-30T12:22:53.785+0000",
"createdBy":"me",
"lastModifiedOn":"2020-09-30T12:22:53.785+0000",
"name":"Unused Template",
"description":"Template"
},
{
"id":10739765484,
"version":70,
"createdOn":"2020-12-21T11:39:51.941+0000",
"createdBy":"creator",
"lastModifiedOn":"2020-12-21T11:39:51.941+0000",
"name":"TestTag202006",
"description":""
}
]
我为此编写了以下课程:
public class TagJSON {
@JsonProperty
private Long id;
@JsonProperty
private Long version;
@JsonProperty
private String createdOn;
@JsonProperty
private String createdBy;
@JsonProperty
private String lastModifiedOn;
@JsonProperty
private String name;
@JsonProperty
private String description;
public TagJSON() {
}
public TagJSON(Long id, Long version, String createdOn, String createdBy, String lastModifiedOn, String name, String description) {
this.id = id;
this.version = version;
this.createdOn = createdOn;
this.createdBy = createdBy;
this.lastModifiedOn = lastModifiedOn;
this.name = name;
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getCreatedOn() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn = createdOn;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getLastModifiedOn() {
return lastModifiedOn;
}
public void setLastModifiedOn(String lastModifiedOn) {
this.lastModifiedOn = lastModifiedOn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "TagJSON{" +
"id=" + id +
", version=" + version +
", createdOn='" + createdOn + '\'' +
", createdBy='" + createdBy + '\'' +
", lastModifiedOn='" + lastModifiedOn + '\'' +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
public class AllTagsJson {
private List<TagJSON> allTags;
public AllTagsJson(List<TagJSON> allTags) {
this.allTags = allTags;
}
public List<TagJSON> getAllTags() {
return allTags;
}
public void setAllTags(List<TagJSON> allTags) {
this.allTags = allTags;
}
@Override
public String toString() {
return "AllTagsJson{" +
"allTags=" + allTags +
'}';
}
}
我想这样运行:
private AllTagsJson doJSONStuff() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
// objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
// AllTagsJson tagJSON = objectMapper.readValue(new File("tag_json.json"), AllTagsJson.class);
List<TagJSON> tags = Arrays.asList(objectMapper.readValue(new File("tag_json.json"), TagJSON[].class));
AllTagsJson allTags = new AllTagsJson(tags);
return allTags;
}
我已经试过了
我得到以下错误:
原因:com.fasterxml.jackson.databind.exc.missmatchdinputException:无法构造的示例 com.example.demo.TagJSON
(尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法从[source:(file)处的字符串值('com.example.demo.tagjson')反序列化;行:1,列:1]
错误怎么可能发生在第1行第1列?在我砸坏电脑之前请帮忙:)
暂无答案!
目前还没有任何答案,快来回答吧!