I have a json in a file name: test.json
{books:{"name":"XXXXX","isbn":98892}}
{books:{"name":"YYYYY","isbn":64728}}
{books:{"name":"ZZZZZ","isbn":19837}}
this is my bean class
class Books{
private String name;
private int isbn;
//getter and setter methods
my main class
Gson gson = new Gson();
try {
BufferedReader br= new BufferedReader(new FileReader("C:\\test.json"));
Books m = gson.fromJson(br, Books.class);
System.out.printf("%s",m.getName());
System.out.printf("\t%d",m.getIsbn());
I am able to print only the first line, if I have to parse other line what should I do ?
As the comment says it is not a valid json string (You can use jsonlint to validate json).
If it the book elements were in a list with commas after each books then you should convert it to a list of Books rather than just use the books class. To do this you would have to use a generic type token .
In this case it would look a bit like this:
Type listOfBooksType = new TypeToken<List<Books>>() {}.getType();
List<Books> books = gson.fromJson(json, listOfBooksType);
(Maybe your class name should be Book rather than Books? - as each "Books" denotes one book)
I presume that you have following json text.
[
{books:{"name":"XXXXX","isbn":98892}},
{books:{"name":"YYYYY","isbn":64728}},
{books:{"name":"ZZZZZ","isbn":19837}}
]
You need to create another class which has books
field,
class Book{
private String name;
private int isbn;
//getter - setter
}
class Books {
private Book books;
public Book getBooks() {
return books;
}
public void setBooks(Book books) {
this.books = books;
}
}
and to parse the json:
BufferedReader br = new BufferedReader(new FileReader("C:\\test.json"));
List<Books> books= gson.fromJson(br,new com.google
.gson
.reflect
.TypeToken<List<Books>>(){}.getType());
for(Books book:books)
{
System.out.println(book.getBooks().getIsbn()
+ " " + book.getBooks().getName());
}
The JSON in your test.json is malformed. It gson shouldn't be able to parse it as it is. Names should be strings, so the data should look like this:
{"books":{"name":"XXXXX","isbn":98892}}
{"books":{"name":"YYYYY","isbn":64728}}
{"books":{"name":"ZZZZZ","isbn":19837}}
But it's a curious data structure anyway. Is it your own format, or something that you've been given? It seems as if it may want to be an array, such as:
{"books":[{"name":"XXXXX","isbn":98892},
{"name":"YYYYY","isbn":64728},
{"name":"ZZZZZ","isbn":19837}]}
If so, you could parse the entire file using Gson, given your Books
class, like this:
Books booksArray[];
booksArray = Gson.fromGson(br, Books[].class);
Otherwise, given the data structure as it is in your question - assuming that the books
attribute names are quoted - you'll need to account for the additional level of object, for example:
class BooksWrapper
{
private Books books;
public Books getBooks()
{
return books;
}
}
And you can loop over the lines in your BufferedReader
and collect all the objects like so:
ArrayList<Books> books = new ArrayList<Books>();
BufferedReader br = new BufferedReader(new FileReader("test.json"));
String line;
BooksWrapper wrapper;
while ((line = br.readLine()) != null)
{
wrapper = gson.fromJson(line, BooksWrapper.class);
books.add(wrapper.getBooks());
}
for (Books book : books)
{
System.out.print(book.getName());
System.out.println("\t" + book.getIsbn());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.