开发者

Problem with beans and Jackson library

开发者 https://www.devze.com 2023-01-05 14:03 出处:网络
HI! I am working with a .json file, like this: [{ \"SourceFile\": \"videos/KobeAlleyOop.flv\", \"ExifTool\": {

HI!

I am working with a .json file, like this:

[{  
  "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"  
  }]  

I made a Bean with 3 components:

 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; 
 }   }

And I add an example of SourceFileBean, the others are similar:

public class SourceFileBean {

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

In the main program I make this call:

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);

But I get this error when I run it, I don't know why:

org.codehaus.jackson.map.JsonMappingException: Can not constr开发者_如何学编程uct 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)

Any help?? Thanks in advance!


I'm guessing that this is just because your JSON represents an array, with a single object inside it. You're asking Jackson to deserialize this array data onto a single instance of MetadataContentBean, which it can't do.

Try removing the [] brackets from around the JSOn, and try again.


The problem was about sintaxis and the way of writting the fields in my program. You must be absotuely sure that it is the SAME as in the json file.

On the other hand

"SourceFile": "videos/KobeAlleyOop.flv"

is a field with just one field, so is not neccesary make a bean for it.

It is a stupid error which could make you waist a lot of time!!! :s


One problem is that you have unnecessary code in there: lines 3 and 4 are not needed and could cause issues. So just do:

  InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);  
  String jsonTxt = IOUtils.toString(is);  
  ObjectMapper mapper = new ObjectMapper(); 
  MetadataContentBean[] beans = mapper.readValue(metadatacontent.toString(),MetadataContentBean[].class);

so you don't have to use json.org's parser in there. This may not explain exact problem but helps avoid secondary issues.

But the specific problem that throws exception is simple(r): JSON value for type is String, but you are trying to make an Object (bean) out of it. To make it work, add a public constructor that takes one String argument, and it should work. You can annotate it with @JsonCreator if you want (or if it's not public constructor), but that should not be necessary.

Conversely, if you want to serialize a bean as JSON String, you need to do something like

@JsonValue public String asString() { return valueOfThisAsString; }
0

精彩评论

暂无评论...
验证码 换一张
取 消