java - 如何使用 Jackson API 从 json 更新现有对象列表

标签 java json spring jackson

我有一个要更新的对象列表。 基本上我已经使用 spring 创建了对象,但对象的内容是空的。

我想使用 Jackson 解析器更新 json 文件中的对象列表。

json 文件与对象兼容。这意味着我让映射器自动检测 setter 。

发生的情况是映射器将对象加载到列表中,如下所示 作为 LinkedHashMap 对象而不是 My 对象

这是我的 json

    [
    {
        "startDate":"01/06/2014 08:00",
        "endDate":"01/06/2014 16:00",
        "shiftType":"Regular",
        "capacity":5
    },
    {
        "startDate":"01/06/2014 16:00",
        "endDate":"01/06/2014 23:00",
        "shiftType":"Regular",
        "capacity":5
    },
    {
        "startDate":"01/06/2014 23:00",
        "endDate":"02/06/2014 08:00",
        "shiftType":"Regular",
        "capacity":5
    },
    {
        "startDate":"02/06/2014 08:00",
        "endDate":"02/06/2014 16:00",
        "shiftType":"Regular",
        "capacity":5
    },
]

这是我的对象

    package il.co.shiftsgenerator.engine.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ShiftConfiguration {

    private int capacity;
    private String shiftType;
    private String startDate;
    private String endDate;
    private SimpleDateFormat dateFormat;
    public int getCapacity() {
        return capacity;
    }
    public String getStartDate() {
        return startDate;
    }
    public void setStartDate(String startDate) throws ParseException {
        dateFormat.parse(startDate);
        this.startDate = startDate;
    }
    public String getEndDate() {
        return endDate;
    }
    public void setEndDate(String endDate) throws ParseException {
        dateFormat.parse(endDate);
        this.endDate = endDate;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public String getShiftType() {
        return shiftType;
    }
    public void setShiftType(String shiftType) {
        this.shiftType = shiftType;
    }
    public SimpleDateFormat getDateFormat() {
        return dateFormat;
    }
    public void setDateFormat(SimpleDateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }
    @Override
    public String toString() {
        return "ShiftConfiguration [capacity=" + capacity + ", shiftType="
                + shiftType + ", startDate=" + startDate + ", endDate="
                + endDate + "]";
    }

}

这就是我尝试加载数据的方式

    ObjectMapper  mapper = new ObjectMapper();
InputStream stream =  fileLoaderHelper.getFileAsStream(SHIFT_CONFIG_LIST_JSON_FILE);
List<ShiftConfiguration> shiftBeans = new ArrayList<ShiftConfiguration>();      

for (int i = 0; i < 21; i++) {
    ShiftConfiguration shiftBean = context.getBean(ShiftConfiguration.class);
    shiftBeans.add(shiftBean);
}

ObjectReader readerForUpdating = mapper.readerForUpdating(shiftBeans);
readerForUpdating.readValues(stream);
System.out.println(shiftBeans);

最佳答案

由于您的日期格式,它可能无法检测到正确的对象。我会尝试明确告诉 jackson 您的约会时间格式,也许这会解决问题。尝试类似的事情

DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
mapper.setDateFormat(df);

在您的mapper.readerForUpdating调用之前看看是否可以。

关于java - 如何使用 Jackson API 从 json 更新现有对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37033228/

相关文章:

java - 无法对数组列表进行排序

java - Android:在ViewAnimator的进出动画之间添加延迟

java ThreadLocal问题

javascript - 为什么我的无序列表的第一项不包含链接?

hibernate - 在 Hibernate + Spring 中使用自定义验证消息

java - 使用 Postgres 在 Spring Data JPA 中的事务中发出脏读

java - 获取键在键盘上的位置

c# - 将数组添加到对象列表

java - 使用gson gradle从JSON读取键和值

spring - Spring tcServer 对 EJB、JAX-WS 的支持