Created
December 7, 2014 03:45
-
-
Save kymmt90/decf11dc917aacc920da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"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