首页 > 解决方案 > 如何更改 Json 格式?

问题描述

我想更改显示 Json 的格式。

我试图打印整个 Item 对象,它正在打印所有属性(看起来很正常)。我尝试更改控制器和服务以返回 String,并尝试操作 String,但我认为这不是解决问题的好方法。一定是更好的东西。

{  
   "requestedUrl":"https://www.googleapis.com/books/v1/volumes?q=java&maxResults=40",
   "items":[  
      {  
         "id":"-SYM4PW-YAgC",
         "volumeInfo":{  
            "title":"The Religion of Java",
            "authors":[  
               "Clifford Geertz"
            ],
            "industryIdentifiers":[  
               {  
                  "type":"ISBN_10",
                  "identifier":"0226285103"
               },
               {  
                  "type":"ISBN_13",
                  "identifier":"9780226285108"
               }
            ],
            "readingModes":{  
               "text":true,
               "image":true
            },
            "pageCount":392,
            "printType":"BOOK",
            "categories":[  
               "Religion"
            ]
         }
      }
   ]
}

BookController.java

@CrossOrigin
@RestController
@RequestMapping("/")
public class BookController {

    private BookService service;

    public BookController(BookService service) {
        this.service = service;
    }

    @GetMapping("book/{isbn}")
    public Item getBook(@PathVariable String isbn) {
        return service.findBookByISBN(isbn);
    }

}

图书服务.java

public class BookService {

    public BookService() throws IOException {
    }


    public Item findBookByISBN(String isbn) {
// listOfItems taking json file from json file and making List<Item> to itarate over 
        for (Item item : listOfItems) {
            if (item.getVolumeInfo().getIndustryIdentifiers().get(0).getIdentifier().equals(isbn)) {
                return item;
            }
        }
        return null;
    }
}

当然,我有 POJO 课......

对于findBookByISBN(9780226285108) json answear 是

{
            "title":"The Religion of Java",
            "authors":[  
               "Clifford Geertz"
            ],
            "industryIdentifiers":[  
               {  
                  "type":"ISBN_10",
                  "identifier":"0226285103"
               },
               {  
                  "type":"ISBN_13",
                  "identifier":"9780226285108"
               }
            ],
            "readingModes":{  
               "text":true,
               "image":true
            },
            "pageCount":392,
            "printType":"BOOK",
            "categories":[  
               "Religion"
            ]
}

但我想让我的 json 像这样:

{  
   "title":"The Religion of Java",
   "printType":"BOOK",
   "pageCount":392,
   "authors":[  
      "Clifford Geertz"
   ],
   "categories":[  
      "Religion"
   ]
}

标签: javajsonspringspring-boot

解决方案


您可以使用数据传输对象 (DTO) 并仅发送所需的信息作为响应。

 {  
   "title":"The Religion of Java", 
   "printType":"BOOK",
   "pageCount":392,
   "authors":[  
     "Clifford Geertz"
    ],
   "categories":[  
    "Religion"
    ]
 } 

在这种情况下,您可以编写一个 DTO,例如

 class NameYouWant{
String title;
String printType;
Integer pageCount;
List<String> authors = new ArrayList<>();
List<String> categories = new ArrayList<>();
//generate getters and setters of the above data members.
 }

现在,当您发送响应时,在此 dto 中设置数据,如下所示:- 创建对象

 public Item findBookByISBN(String isbn) {
    for (Item item : listOfItems) {
        if (item.getVolumeInfo().getIndustryIdentifiers().get(0).getIdentifier().equals(isbn)) {
           NameYouWant na = new NameYouWant();
           na.setTitle(item.getTitle());
           na.setPrintType(item.getPrintType());
           na.setPageCount(item.getPageCount());
           na.SetAuthors(item.getAuthors());
           na.SetCategories(item.getCategories());
            return na;
        }
    }
    return null;
}

通过这种方式,您可以只在 forntend 中发送所需的数据。


推荐阅读