Mapping JSON data to Java objects -


i have been trying map json data java objects, json file on pc, throws exception:

org.codehaus.jackson.map.exc.unrecognizedpropertyexception: unrecognized field "title" (class movieresponse), not marked ignorable @ [source: c:\m.json; line: 2, column: 13] (through reference chain: movieresponse["title"])

my data class:

import org.codehaus.jackson.annotate.jsonproperty;  public class movieresponse{   private string title;   private integer year;   @jsonproperty("mpaa_rating")   private string mpaarating;   private integer runtime;   @jsonproperty("critics_consensus")   private string criticsconsensus;    public string gettitle(){     return title;   }   public string settitle(string t){     return title = t;   }    public integer getyear(){     return year;   }   public integer setyear(integer y){     return year = y;   }    public string getmpaa(){     return mpaarating;   }   public string setmpaa(string mp){     return mpaarating = mp;   }    public integer getruntime(){     return runtime;   }   public integer setruntime(integer r){     return runtime = r;   }    public string getcritics(){     return criticsconsensus;   }   public string setcritics(string c){     return criticsconsensus = c;   }    @override   public string tostring(){     return "movieresponse[title="+title+",year="+year+",mpaa_rating="+mpaarating+",runtime="+runtime+",critics_consensus="+criticsconsensus             +"]";   } } 

my mapper class:

import java.io.file; import java.io.ioexception; import org.codehaus.jackson.jsongenerationexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; import java.net.*; import org.codehaus.jackson.annotate.jsonignoreproperties; @jsonignoreproperties public class movie{  public static void main(string[] args) throws malformedurlexception, urisyntaxexception, ioexception {  movieresponse = new movieresponse();  objectmapper mapper = new objectmapper();  try{       movieresponse response = new objectmapper().readvalue(new file("c:\\m.json"), movieresponse.class);  }catch(malformedurlexception u){     u.printstacktrace();  }  catch(ioexception i){     i.printstacktrace();  }  } 

the json file contains following data:

{   "title": "toy story 3",   "year": 2010,   "mpaa_rating": "g",   "runtime": 103,   "critics_consensus": "deftly blending comedy, adventure, , honest emotion, toy story 3 rare second sequel works." } 

what doing wrong? using jackson library.

here list of problems see in code:

  1. the @jsonignoreproperties attribute should put above movieresponse class, not movie class. check out documentation, notably said "ignoreunknown" property, defaulted false:

    public abstract boolean ignoreunknown

    property defines whether ok ignore unrecognized properties during deserialization. if true, properties unrecognized -- is, there no setters or creators accept them -- ignored without warnings (although handlers unknown properties, if any, still called) without exception.

  2. your setters should not return value, may explain why jackson not see "title" setter. here how setter "title" should be:

    public void settitle(string t) {     title = t; } 
  3. not reason not work, declaring object mapper twice, consider using mapper instead of instanciating new one:

    movieresponse response = mapper.readvalue(new file("c:\\m.json"), movieresponse.class); 

edit: here's fixed code think:

import java.io.file; import org.codehaus.jackson.map.objectmapper; import java.net.*; import org.codehaus.jackson.annotate.jsonignoreproperties;  public class movie {     public static void main(string[] args) throws exception {         movieresponse response;         objectmapper mapper = new objectmapper();          response = mapper.readvalue(new file("c:\\m.json"), movieresponse.class);         system.out.println(response);     } } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -