gpt4 book ai didi

java - bean 和 Jackson 库的问题

转载 作者:行者123 更新时间:2023-11-29 09:25:02 25 4
gpt4 key购买 nike

嗨!

我正在处理 .json 文件,如下所示:

[{  
"SourceFile": "videos/KobeAlleyOop.flv",
"ExifTool": {
"ExifToolVersion": 8.22,
"Warning": "Truncated 'mdat' data"
},
"System": {
"FileName": "KobeAlleyOop.flv",
"Directory": "videos",
"FileSize": "4.8 MB",
"FileModifyDate": "2010:06:15 14:57:24+02:00",
"FilePermissions": "rwxr-xr-x"
},
"File": {
"FileType": "MP4",
"MIMEType": "video/mp4"
}]

我制作了一个包含 3 个组件的 Bean:

 public class MetadataContentBean {   
SourceFileBean sourceFileBean;
FileBean fileBean;
SystemBean systemBean;

public FileBean getFileBean() { return fileBean; }
@JsonProperty("File")
public void setFileBean(FileBean fileBean) {
this.fileBean = fileBean; }
public SystemBean getSystemBean() {
return systemBean; }
@JsonProperty("System")
public void setSystemBean(SystemBean systemBean) {
this.systemBean = systemBean; }
public SourceFileBean
getSourceFileBean() {
sourceFileBean.getSource(); return
sourceFileBean; }
@JsonProperty("SourceFile")
public void setSourceFileBean(SourceFileBean
sourceFileBean) {
this.sourceFileBean = sourceFileBean;
} }

并且我添加了一个SourceFileBean的例子,其他类似:

public class SourceFileBean {

private String source;
public String getSource() {
return source;
}
@JsonProperty("SourceFile")
public void setSource(String source) {
this.source = source;
}
}

在主程序中我调用了:

InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);  
String jsonTxt = IOUtils.toString(is);
JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
JSONObject metadatacontent = json.getJSONObject(0);
ObjectMapper mapper = new ObjectMapper(); mapper.readValue(metadatacontent.toString(),MetadataContentBean.class);

但是我运行的时候出现这个错误,我不知道为什么:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.path.bean.SourceFileBean, problem: no suitable creator method found at [Source: java.io.StringReader@12d7a10; line: 1, column: 2] at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:159) at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:212) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.java:415) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:291) at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:135) at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:221) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:390) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:286) at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1588) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1116) at com.path.parser.JSon.Parser(JSon.java:65) at com.path.parser.JSon.main(JSon.java:29)

有什么帮助吗?提前致谢!

最佳答案

我猜这只是因为您的 JSON 表示一个数组,其中包含一个对象。您要求 Jackson 将此数组数据反序列化到 MetadataContentBean 的单个实例中,这是它做不到的。

尝试从 JSOn 周围移除 [] 括号,然后重试。

关于java - bean 和 Jackson 库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3156962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com