[英]Convert JSON to Map<String, List<Object>> in java
[英]Convert List<Map<String, String>> to Object in Java Spring boot jpa
我有一個問題如何將List<Map<String, String>>
轉換為自定義 class 對象以將其保存到 JPA。 如果我應用branchRepository.save(content)
它不起作用。
這是我的代碼:
-- BranchService.java --
public List<Map<String, String>> uploadEmployee(MultipartFile multip) throws Exception {
String fileNames = multip.getOriginalFilename();
DataFormatter formatter = new DataFormatter();
File file = new File("./reports/" + fileNames);
Workbook workbook = WorkbookFactory.create(file);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
int headerRowNum = sheet.getFirstRowNum();
Map<Integer, String> colHeaders = new HashMap<Integer, String>();
Row row = sheet.getRow(headerRowNum);
for (Cell cell : row) {
int colIdx = cell.getColumnIndex();
String value = formatter.formatCellValue(cell, evaluator);
colHeaders.put(colIdx, value);
}
List<Map<String, String>> content = new ArrayList<Map<String, String>>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
row = sheet.createRow(r);
Map<String, String> valuesToHeaders = new HashMap<String, String>();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell == null)
cell = row.createCell(colIdx);
String cellValue = formatter.formatCellValue(cell, evaluator);
valuesToHeaders.put(entry.getValue(), cellValue);
}
content.add(valuesToHeaders);
}
workbook.close();
System.out.println(content);
return content;
}
如何將其轉換並應用到 JPA?
如果 Map 作為結果列表的元素,包含映射到其值的實體屬性,則可以使用 Gson 來創建 JPA 實體的實例:
Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(map);
MyEntity pojo = gson.fromJson(jsonElement, MyEntity.class);
而不是產生像Map
List
這樣通用的東西,而是直接返回您的 JPA 實體。
所以轉這個:
List<Map<String, String>> content = new ArrayList<Map<String, String>>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
row = sheet.createRow(r);
Map<String, String> valuesToHeaders = new HashMap<String, String>();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell == null)
cell = row.createCell(colIdx);
String cellValue = formatter.formatCellValue(cell, evaluator);
valuesToHeaders.put(entry.getValue(), cellValue);
}
content.add(valuesToHeaders);
}
變成更像這樣的東西:
List<Branch> content = new ArrayList<>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
continue; //SKIP, don't bother creating empty stuff!
Branch branch = new Branch();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell != null) {
String cellValue = formatter.formatCellValue(cell, evaluator);
switch(entry.getValue()) {
case "Description": {
branch.setDescription(cellValue);
break;
}
case "name": //example with multiple headers mapping to same field
case "Label": {
branch.setLabel(cellValue);
break;
}
}
//alternatively use if-else block with regex matching or some other technique to map your headers to JPA entity fields
}
}
content.add(branch);
}
使用switch
您可以 map 多個不同的拼寫或縮寫(如果您的規范允許這種事情),但為了獲得最大的靈活性,您可以使用正則表達式執行 if/else。 也可以完全取消標頭並基於此索引( switch
colIdx
)。
當然,您可以保持您的代碼原樣並將上述代碼寫入新的轉換 function,例如:
List<Branch> convert(List<Map<String,String>> content) {
return content.stream().map(this::convert).collect(Collectors.toList());
}
Branch convert(Map<String,String> props) {
Branch branch = new Branch();
for(String key : props.keySet()) {
String value = props.get(key);
switch(key) {
case "Description": {
branch.setDescription(value);
break;
}
case "name": //example with multiple headers mapping to same field
case "Label": {
branch.setLabel(value);
break;
}
}
}
return branch;
}
這可能是最整潔的選項(盡管您仍然希望刪除空行和單元格創建代碼)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.