java - 使用 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中的totalTime为1H10M ,然后我们将其分配为“ totalTime:7 ”。如果是“ 30M ”,我们可以将其指定为“ totalTime:3 ”。我想用java来做到这一点。
所需输出
"Rating":
{
"ratingValue": "4.636365",
},
"Timings":
{
"totalTime": "7"
}
解决方案
我试过这个:
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"
}
}
推荐阅读
- java - 使用 LittleProxy 拦截、操作和转发请求
- amazon-web-services - 使用 eventbridge 检测 cloudwatch 日志中的错误
- c# - 将 URL 限制为特定数据库用户名 ASP.NET
- linux - 如何在禁用 SMP 支持的情况下运行“invd”指令?
- java - 如何将子类的字段值与超类字段值一起存储到 ArrayList 中?- 爪哇
- javascript - 以编程方式触发 React-Select 以选择/提交顶部选项?
- string - 为什么我无法重新分配这个基本变量值(GODOT 游戏引擎)
- laravel - 如何混合使用 Laravel 路由和 Vue 路由?
- laravel - ID 为空时如何使用 Laravel 的 updateOrCreate() 方法创建实例?
- r - 如何使用 ggpredict 创建图形