首页 > 解决方案 > 使用 Spring Java 将 JSON 属性映射到整数

问题描述

我有很多以下格式的 JSON 文件。我想将一个名为Timings的属性映射到integer

测试.json

"Rating": {
  "ratingValue": "4.636365",
  "bestRating": "5",
  "worstRating": "1",
  "ratingCount": "66"
 },
 "Timings": {
  "cookTime": "PT1H",
  "prepTime": "PT10M",
  "totalTime": "PT1H10M"
 }

我想在映射后将输出存储在另一个 JSON 文件中。假设Timings中的totalTime1H10M ,然后我们将其分配为“ totalTime:7 ”。如果是“ 30M ”,我们可以将其指定为“ totalTime:3 ”。我想用java来做到这一点。

所需输出

"Rating": 
{
  "ratingValue": "4.636365",
},
 "Timings": 
{
    "totalTime": "7"
}

标签: javajsonspringjackson

解决方案


我试过这个:

class Timings {

    private String cookTime;
    private String prepTime;
    private String totalTime;

    public String getCookTime() {
        return cookTime;
    }

    public void setCookTime(String cookTime) {
        this.cookTime = cookTime;
    }

    public String getPrepTime() {
        return prepTime;
    }

    public void setPrepTime(String prepTime) {
        this.prepTime = prepTime;
    }

    public String getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(String totalTime) {
        this.totalTime = totalTime;
    }

    @Override
    public String toString() {
        return "Timings [cookTime=" + cookTime + ", prepTime=" + prepTime + ", totalTime=" + totalTime + "]";
    }

}
public class Test {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Timings obj = mapper.readValue(new File("C:\\Users\\Anish\\Desktop\\abc.json"), Timings.class);
        String totalTime = obj.getTotalTime().split("PT")[1];
        int total = 0;
        if (totalTime != null && !totalTime.isEmpty()) {
            total = returnTotalTime(totalTime);
        }
        ObjectNode mainNode = mapper.createObjectNode();
        ObjectNode timingNode = mapper.createObjectNode();
        childNode.put("totalTime", (total > 9) ? (total / 10) : total);
        mainNode.set("Timings", timingNode);
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mainNode);
        System.out.println(json);

    }

    private static int returnTotalTime(String totalTime) {
    if (totalTime != null && !totalTime.isEmpty()) {
        String[] timeValues = totalTime.split("H");
        if (timeValues.length == 0) {
            return 0;
        } else if (timeValues.length < 2) {
            if (timeValues[0].contains("M")) {
                return (timeValues[0].split("M").length <= 0) ? 0
                        : timeValues[0].split("M")[0].isEmpty() ? 0 : Integer.parseInt(timeValues[0].split("M")[0]);
            }
            return timeValues[0].isEmpty() ? 0 : Integer.parseInt(timeValues[0]) * 60;
        }
        int hour = timeValues[0].isEmpty() ? 0 : Integer.parseInt(timeValues[0]) * 60;
        int mins = (timeValues[1].split("M").length <= 0) ? 0
                : timeValues[1].split("M")[0].isEmpty() ? 0 : Integer.parseInt(timeValues[1].split("M")[0]);
        return (hour + mins);
    }
    return 0;
}

abc.json

{
  "cookTime": "PT1H",
  "prepTime": "PT10M",
  "totalTime": "PT1H10M"
}

输出 :

{
  "Timings" : {
    "totalTime" : "7"
  }
}

"totalTime": "PT30M", 那么 :

输出 :

{
  "Timings" : {
    "totalTime" : "3"
  }
}

"totalTime": "PT23M", 那么 :

输出 :

{
  "Timings" : {
    "totalTime" : "2"
  }
}

推荐阅读