Java Json程序没有显示任何输出

我想要做的是,当我导航到:

http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[your_api_key]&q=Toy+Story+3&page_limit=1

它返回一个JSON响应。 我想将其转换为Java Object。 我正在使用jackson图书馆。 这是我的数据类:

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 + "]"; } } 

和我的mapper类:

 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(); } } } 

它没有给我任何输出。 难道我做错了什么?

您混合了基本的Java编程和JSON映射错误。 首先,请记住在定义Java类时遵循较低的camel case命名约定 。 它不仅是最佳实践,更重要的是,如果您不遵循这种模式(包括Jackson),几个工具和库将无法工作

就JSON映射错误而言,将JSON映射到Java对象时要记住的最重要的事情是JSON数据实际上是一个映射。 它将键与值相关联,其中值可以是基元,对象或数组(集合)。 因此,给定一个JSON结构,您必须查看每个键值的结构,然后决定该值应该在Java中表示为基元对象还是任何一个数组 。 没有捷径可言,您将通过经验学习。 这是一个例子:

 { "total": 1, // maps to primitive, integer "movies": [ // '[' marks the beginning of an array/collection { // '{' marks the beginning of an object "id": "770672122", // maps to primitive, string "title": "Toy Story 3", "year": 2010, "mpaa_rating": "G", "runtime": 103, "release_dates": { // each array object also contains another object "theater": "2010-06-18", "dvd": "2010-11-02" } } ] } 

映射上面的示例JSON时,您需要定义与最外层{ 。匹配的根对象。 让我们将这个根对象称为MovieResponse

 public class MovieResponse { } 

现在,沿着JSON数据,我们开始将所有JSON属性映射到Java属性:

 @JsonIgnoreProperties(ignoreUnknown = true) public class MovieResponse { private Integer total; // map from '"total": 1' private List movies; // map from '"movies": [', remember, its a collection } 

简单吧? 但是,当然,我们还需要为Movie类定义一个结构。 再一次,走JSON:

 @JsonIgnoreProperties(ignoreUnknown = true) public class Movie { private String id; // map from '"id": "770672122"' private String title; // map from '"title": "Toy Story 3"' private Integer year; @JsonProperty("mpaa_rating") private String mpaaRating; private Integer runtime; @JsonProperty("release_dates") private Release released; // another object mapping! } 

最后,映射代表发布日期的最内层对象:

 public class Release { private String theater; private String dvd; } 

多数民众赞成,非常直截了当。 注意在Movie类中使用@JsonProperty 。 它允许您使用不同的名称将JSON中的属性映射到Java中的属性。 另请注意,为简洁起见,上述每个类都省略了构造函数/ setter / getter。 在您的真实代码中,您将添加它们。最后,您将使用以下代码将示例JSON映射到Java MovieResponse类:

 MovieResponse response = new ObjectMapper().readValue(json, MovieResponse.class);