Skip to content

Instantly share code, notes, and snippets.

@kymmt90
Created December 7, 2014 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kymmt90/decf11dc917aacc920da to your computer and use it in GitHub Desktop.
Save kymmt90/decf11dc917aacc920da to your computer and use it in GitHub Desktop.
import java.io.File;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Books {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
List<Book> books = mapper.readValue(new File("books.json"), new TypeReference<List<Book>>() {});
books.stream().forEach(System.out::println);
}
}
final class Book {
private String title;
private String author;
private String year;
@JsonCreator
public Book(@JsonProperty("title") String title,
@JsonProperty("author") String author,
@JsonProperty("year") String year) {
this.title = title;
this.author = author;
this.year = year;
}
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getYear() { return year; }
@Override
public String toString() {
return "[" + title + "," + author + "," + year + "]";
}
}
[
{
"title":"Harry Potter and the Philosopher's Stone",
"author":"J.K. Rowling",
"year":"1997"
},
{
"title":"Effective Java",
"author":"Joshua Bloch",
"year":"2008"
},
{
"title":"Effective C++",
"author":"Scott Meyers",
"year":"2005"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment