带有jackson的动态对象名称

时间:2016-06-02 13:09:14

标签: java json jackson

我有课程:

public class dayRate {

private String date;
private List<Rate> currencyList = new ArrayList()
}

public class Rate {

private String currency;
private BigDecimal cashBuy;
private BigDecimal cashSell;
}

我需要像这样生成一个JSON:

{ "list" : {
"2016-10-31": {
    EUR: {rateCashBuy: "", rateCashSell: "" },
    USD: {rateCashBuy: "", rateCashSell: "" },
},
"2016-11-01": {
    EUR: {cashBuy: "", cashSell: "" },
    USD: {cashBuy: "", cashSell: "" },
},
"2016-11-02": {
    EUR: {cashBuy: "", cashSell: "" },
    USD: {cashBuy: "", cashSell: "" },
}
}

如果我的JSON中没有单词“date”和“currencyList”,我该怎么办呢? 我怎样才能使用日期的值?

我现在有这样的代码:

private List<DayRate> formDayRateList(List<CurrencyConvertionRate> list) {
    List<DayRate> dayRateList = new ArrayList<>();

    String date = list.get(0).getCreated().toLocalDateTime().toLocalDate().toString();
    DayRate dayRate = new DayRate();
    dayRate.setDate(date);

    for (CurrencyConvertionRate ccr : list) {
        String newDate = ccr.getCreated().toLocalDateTime().toLocalDate().toString();
        if (!date.equals(newDate)) {
            dayRateList.add(dayRate);
            dayRate = new DayRate();
            dayRate.setDate(newDate);
        }
        Rate rate = new Rate();
        rate.setCurrency(ccr.getCurrencyCode());
        rate.setRateCashBuy(ccr.getRateCashBuy());
        rate.setRateCashSell(ccr.getRateCashSell());
        dayRate.add(rate);

        date = ccr.getCreated().toLocalDateTime().toLocalDate().toString();
    }

    dayRateList.add(dayRate);

    return dayRateList;
}

结果:

"list":
   [{"date":"2016-10-31",
   "currencyList":
   [{"currency":"EUR", "cashBuy":00.00,"cashSell":00.00},
   {"currency":"GBP" .. 

所以,这不是我需要的

0 个答案:

没有答案