tags:

views:

86

answers:

3

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 construct instance of com.playence.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.playence.parser.JSon.Parser(JSon.java:65) at com.playence.parser.JSon.main(JSon.java:29)

Any help?? Thanks in advance!

A: 

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.

skaffman
But that is not a right solution, because json files are generated automatically, and I have a lot of them.Anyway, I tryed with the same result...:sThanks anyway
mujer esponja
Anyway, MetadataContentBean is a JSONObject, and it takes one element from the array, so it shouldn't be a problem, should it?
mujer esponja
A: 

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

mujer esponja
:-)See my answer, still; it is true that you don't need a Bean, but if you must, it can be made to work as well.
StaxMan
A: 

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