Question

Je suis en train d'analyser un fichier JSON comme celui-ci, généré par exiftool:

[{
  "SourceFile": "videos/XaviHernandez.flv",
  "ExifTool": {
    "ExifToolVersion": 8.22
  },
  "System": {
    "FileName": "XaviHernandez.flv",
    "Directory": "videos",
    "FileSize": "16 MB",
    "FileModifyDate": "2010:06:17 09:57:21+02:00",
    "FilePermissions": "rw-r--r--"
  },
  "File": {
    "FileType": "FLV",
    "MIMEType": "video/x-flv"
  }
}]

Dans un bean Java avec cette structure:

public class MetadataContentBean {
    ExifToolBean exiftoolBean;
    String SourceFile;
    FileBean fileBean;
    SystemBean systemBean;
//Getters and setter
}

Mon code java est la suivante:

    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();
    MetadataContentBean meta = new MetadataContentBean();
    mapper.readValue(metadatacontent.toString(), MetadataContentBean.class);
    meta= (MetadataContentBean) JSONObject.toBean(metadatacontent, MetadataContentBean.class);

Mais je reçois cette erreur:

net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'ExifTool'
    at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:45)
    at net.sf.json.JSONObject.setProperty(JSONObject.java:1477)
    at net.sf.json.JSONObject.toBean(JSONObject.java:468)
    at net.sf.json.JSONObject.toBean(JSONObject.java:253)
    at com.playence.parser.JSon.Parser(JSon.java:66)
    at com.playence.parser.JSon.main(JSon.java:28)
Caused by: java.lang.NoSuchMethodException: Unknown property 'ExifTool'

J'ai vérifié dans plusieurs forums, mais la solution est-ce, donc je ne sais pas pourquoi je ne comprends pas.

Toute idée?

Était-ce utile?

La solution

ObjectMapper mapper = new ObjectMapper();
MetadataContentBean meta= mapper.readValue(metadatacontent.toString(), MetadataContentBean.class);

Dans cette méta est toutes les informations

Autres conseils

Les mélanges question com.fasterxml.jackson et net.sf.json bibliothèques interchangeables.

@Blanca a donné réponse à jackson. Et voici l'alternative net.sf.json:

JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
JSONObject metadatacontent = json.getJSONObject(0);
MetadataContentBean meta = (MetadataContentBean) JSONObject.toBean(metadatacontent, MetadataContentBean.class);

Le NoSuchMethodException: Unknown property 'ExifTool' a été jeté parce que PropertySetStrategy. DEFAULT requiert des champs publics ou setters, je suppose.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top