Json Parsing through multiple object

做~自己de王妃 提交于 2019-12-12 01:42:40

问题


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 ?


回答1:


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)




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/11698447/json-parsing-through-multiple-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!