如何在 java 中显示使用 GSON 解析的字符串对象时去掉“\”

How to get rid of the "\" while displaying a string object parsed with GSON in java

我正在尝试使用 GSON 获取 JSON 个对象(书籍)的清晰数组。 但是每当我 运行 以下代码时:

public class Book {
    private int id;
    private String name;
    private String type;
    private boolean available;
    
    public Book(int id, String name, String type, boolean available) {
        super();
        this.id = id;
        this.name = name;
        this.type = type;
        this.available = available;
    }

    @Override
    public String toString() {
        return "{id=" + id + ", name=" + name + ", type=" + type + ", available=" + available + "},";
    }

    
}
import org.springframework.stereotype.Component;

import com.google.gson.Gson;

@Component
public class BookService {

    private int size = 0;
    public  int len = 6;
    public Book Allbooks[] = new Book[len];

    public Book AddBook(int id, String name, String type, boolean available) {
        if(id == len)
            return null;
        this.size++;
        return Allbooks[this.size - 1] = createBook(id, name, type, available);


    }
    private Book createBook(int id, String name, String type, boolean available) {
        return new Book(id, name, type, available);
    }
    public Book[] bookRepo() {
        
        Allbooks[0] = AddBook(1, "One", "fiction", false);
        Allbooks[1] = AddBook(2, "Two", "non-fiction", true);
        Allbooks[2] = AddBook(3, "Three", "fiction", true);
        Allbooks[3] = AddBook(4, "Four", "fiction", false);
        Allbooks[4] = AddBook(5, "Five", "non-fiction", true);
        Allbooks[5] = AddBook(6, "Six", "non-fiction", true);

        return Allbooks;
    }
    public String[] showAll() {
        String[] arr = new String[Allbooks.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Gson().toJson(Allbooks[i], Book.class);
        }return arr;

    }
}
@RestController
class ControllerGET {

    @Autowired Status status;
    @Autowired BookService bookRepository;

    @GetMapping(path = {"/", ""})
    public String mainPage() {
        return "Welcome to Mile's simple book-API";
    }
    @GetMapping(path = "/status")
    public Status getStatus() {
        status.value = "OK";
        return status;
    }
    @GetMapping(path = "/books")
    public String[] getAllBooks() {
         bookRepository.bookRepo();
         return bookRepository.showAll();
    }
    //@GetMapping(path = "/books/:bookid")
    
}

我在本地主机 (Postman) 上得到以下输出:

[
    "{\"id\":1,\"name\":\"One\",\"type\":\"fiction\",\"available\":false}",
    "{\"id\":2,\"name\":\"Two\",\"type\":\"non-fiction\",\"available\":true}",
    "{\"id\":3,\"name\":\"Three\",\"type\":\"fiction\",\"available\":true}",
    "{\"id\":4,\"name\":\"Four\",\"type\":\"fiction\",\"available\":false}",
    "{\"id\":5,\"name\":\"Five\",\"type\":\"non-fiction\",\"available\":true}",
    "null"
]

我的目标是让它看起来像这样:

 {"id":1,
  "name":"One",
  "type":"fiction",
  "available":false}",
}

其他书也一样。如果您知道方法,请帮助我。我将不胜感激! ..................................... ..................................... ..................................... ..................................... .....................

在 springboot 上,您不需要在 return 之前解析为 json。 Spring boot 会为你做这件事(默认使用 jackson 库)

试试这个:


  @GetMapping(path = "/books")
    public Book[] getAllBooks() { // return Book[] instead of String []
         bookRepository.bookRepo();
         return bookRepository.bookRepo(); // return all list of books
    }

删除@Override toString()函数。直接return把gson().toJson()转成字符串

不过这样不好。您可以定义 ArrayList 而不是原始数组。所以,

ArrayList<Book> AllBooks;
AllBooks.add(new Book(// add book 1));
AllBooks.add(new Book(// add book 2));
AllBooks.add(new Book(// add book 3));

然后在 repo 上你可以 return

return new Gson.toJson(AllBooks);

然而

这不是最佳做法,因为您的图书存储在服务器的 RAM 上。因此,重新考虑建立一个数据库来辅助数据关系。事实上,spring 提供了 jpa 的 @Entity 来帮助您完成这种事情。当您将图书声明为实体时,例如

@Entity
public class Book {
    private int id;
    private String name;
    private String type;
    private boolean available;
}

您可以创建一个 JpaRepository 来检索所有数据,例如

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
}

然后只需在您的控制器上调用存储库

@GetMapping(path = "/books")
public List<book> getAllBooks() {
    BookRepository bookRepo = new BookRepository();
    List<book> books = bookRepo.findAll();
    return books;
}