我想要做的是,当我导航到:
http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[your_api_key]&q=Toy+Story+3&page_limit=1
它返回一个 JSON 响应。我想把它转换成 Java 对象。我正在使用杰克逊图书馆。这是我的数据类:
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 SetId(int i){ id = i; }
public void SetYear(int y){ year = y; }
public void SetTotal(int t){ total =t; }
public int GetId()
{
return id;
}
public int GetYear()
{
return year;
}
public int GetTotal()
{
return total;
}
@Override
public String toString()
{
return "Movies[total=" +total + "id=" + id + "year=" + year + "]";
}
}
和我的映射器类:
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();
try
{
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);
}catch(MalformedURLException u)
{
u.printStackTrace();
}catch(URISyntaxException s)
{
s.printStackTrace();
}catch(IOException i)
{
i.printStackTrace();
}
}
}
它显示我没有任何输出。难道我做错了什么?