2

This is my Aman class:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class  Aman
{

public static class Title
{
    private String title;

    public String getTitle(){ return title; }

    public String setTitle(String s){ return title = s; }
}

private int id;
private int year;
private int total;

public void id(int i){ id = i; }

public void year(int y){ year = y; }

public void total(int t){ total =t; }
}

This is the Movie class that is mapping the values:

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.*;

public class Movie
{
 public static void main(String[] args) throws MalformedURLException, URISyntaxException, IOException {
 Aman a = new Aman();
 ObjectMapper mapper = new ObjectMapper();
 URL RT = new URL("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=gzscv4f8zqse75w94mmp37zz&q=Toy+Story+3&page_limit=1").toURI().toURL();
 a = mapper.readValue(RT, Aman.class);
    }

}

I am getting the same exception over and over again.

Am I doing something wrong?

Here is the exception that keeps occuring:

Exception:Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "total" (Class Aman), not marked as ignorable at [Source: http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=gzscv4f8zqse75w94mmp37zz&q=Toy+Story+3&page_limit=1; line: 31, column: 11] (through reference chain: Aman["total"])

Owen Versteeg
  • 342
  • 2
  • 14
Aman Grover
  • 1,621
  • 1
  • 21
  • 41

2 Answers2

3

To the top of your Aman class, add the following annotation - @JsonIgnoreProperties(unknown=true). That, or actually map all the attributes present in the JSON as properties in the class.

@JsonIgnoreProperties(ignoreUnknown=true)
class Aman {
    // properties
}

Some other options for managing non-mapped attributes are described on the Jackson FasterXML Wiki.


Edit:

Based on your comments, the data class you are mapping the JSON too has no properties whatsoever. This is not going to work. I would recommend reading through the 5 Minute Jackson Tutorial on FasterXML, but the general gist of mapping is this:

Given JSON document:

{
    "message" : "Hi, I am a JSON object",
    "sender" : "Bob"
}

And Java object:

@JsonIgnoreProperties(unknown=true)
class Post {
    private String message;
}

You can write the following code to map the JSON to the object, using Jackson:

final ObjectMapper mapper = new ObjectMapper();
final Post post = mapper.readValue(json, Post.class);

Assume that json is a string containing the JSON data shown

Jackson will map the attributes in the JSON to the properties in the Java object. In the example shown, there is an additional attribute in the JSON called sender. Ordinarily the library would throw an exception if there was not a corresponding property with the same name on your Java object, but with the @JsonIgnoreProperties(unknown=true) annotation this behavior is overriden, and instead the deserialization continues.

So what you have to do is define an Aman class that has properties matching the JSON that the Rotten Tomatoes API is sending you.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195
  • Let's see the definition of your `Aman` class then, add it to the question. Also, noticed that you have an error in your code, the mapping line should be `Aman a = mapper.readValue(RT, Aman.class);`. – Perception Apr 14 '13 at 19:42
  • I added the class definition now. – Aman Grover Apr 14 '13 at 20:43
  • @AmanGrover - actually, need to see your `Aman` class. The one that data is being mapped into. – Perception Apr 14 '13 at 20:45
  • well this is my Aman class. Now I am really confused as I don't know what I am doing. Simply , what I want is , when I navigate to : http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[your_api_key]&q=Toy+Story+3&page_limit=1 it returns json data, I want to convert it to JAVA object, but I think in the above code I am doing something blunder. – Aman Grover Apr 14 '13 at 20:54
  • Oh, well thats a problem. Your class has no attributes whatsoever. – Perception Apr 14 '13 at 21:02
  • Any solution how I can acheive what I want? I am new to java, so it always takes me long to understand these new concepts. – Aman Grover Apr 14 '13 at 21:07
  • I added some detail to my answer, but now its on you to read through the linked tutorial. Its *very* informative. – Perception Apr 14 '13 at 21:11
  • Thanks a lot. It's lot easier to understand what you have written , than what is written at the Jackson website. – Aman Grover Apr 14 '13 at 21:14
  • I have done everything you said , but still getting the same exception. I just need a little more help. – Aman Grover Apr 15 '13 at 11:46
  • @AmanGrover - please ask a new question with the current problems you're having, since you would agree this question has already changed considerably from its original posted form. – Perception Apr 15 '13 at 11:53
  • I posted the new question at : http://stackoverflow.com/questions/16019117/java-json-program-not-showing-any-output – Aman Grover Apr 15 '13 at 15:32
0

Your Aman class that is supposed to receive the data from the remote has no field total, while the JSON that you have received has this field.

You can either just add it to the class or tell Jackson to ignore unknown things that it receives by giving:

mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

before running mapper.readValue().

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119