简体   繁体   English

Json通过多个对象解析

[英]Json Parsing through multiple object

I have a json in a file name: test.json 我在文件名中有一个json:test.json

{books:{"name":"XXXXX","isbn":98892}}
{books:{"name":"YYYYY","isbn":64728}}
{books:{"name":"ZZZZZ","isbn":19837}}

this is my bean class 这是我的bean类

    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). 正如评论所说,它不是有效的json字符串(您可以使用jsonlint来验证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. 如果将book元素放在每本书后的逗号列表中,则应将其转换为Books列表,而不仅仅是使用books类。 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) (也许您的班级名称应该是Book而不是Books?-每个“ Books”都表示一本书)

I presume that you have following json text. 我假设您有以下json文本。

[
{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, 您需要创建另一个包含“ books字段的类,

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: 并解析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. 您的test.json中的JSON格式错误。 It gson shouldn't be able to parse it as it is. gson应该无法原样解析它。 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类,使用Gson解析整个文件,如下所示:

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: 否则,给定问题所在的数据结构-假设books属性名称已加引号-您将需要考虑对象的附加级别,例如:

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: 您可以在BufferedReader循环浏览并收集所有对象,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM