将Pojo转换为JSON

时间:2019-02-12 14:12:18

标签: java jackson xhtml thymeleaf pojo

我做文档。在pdf中,我的对象应类似于json。 我创建了一个对象集合:

Arrays.asList(new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),
              new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),
              new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7))
)

CoefficientPerQuantityParameter看起来像这样

public class CoefficientPerQuantityParameter {
    private Integer hour;
    private BigDecimal cost;
}

我在xhtml文件中接受它:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <title>My document</title>
    </head>
    <body>
        <div>
             <p th:text="${coefficientPerQuantityParameter}"/>
        </div>
    </body>
</html>

我需要以JSON的形式查看结果。但是我看到了完全不同的东西:

[CoefficientPerQuantityParameter
(hour=2, cost=0.9),
CoefficientPerQuantityParameter
(hour=10, cost=0.8),
CoefficientPerQuantityParameter
(hour=40, cost=0.7)]

如何获取?

{"2": 0.9,  "10": 0.8,   "40": 0.7} 

2 个答案:

答案 0 :(得分:0)

POJO实例列表上方序列化的最经典的方法是将其序列化为数组。

import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class MapperApp {

    public static void main(String[] args) throws Exception {
        List<CoefficientPerQuantityParameter> coefficients = Arrays.asList(new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),
                new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),
                new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7)));

        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(coefficients));
    }
}

上面的代码显示:

[ {
  "hour" : 2,
  "cost" : 0.9
}, {
  "hour" : 10,
  "cost" : 0.8
}, {
  "hour" : 40,
  "cost" : 0.7
} ]

如果您想要一个结构,其中hour是键,而cost是一个值,我建议手动将给定的数组转换为Map并序列化结果。

import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MapperApp {

    public static void main(String[] args) throws Exception {
        List<CoefficientPerQuantityParameter> coefficients = Arrays.asList(new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),
                new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),
                new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7)));

        Map<Integer, BigDecimal> map = coefficients.stream()
                .collect(Collectors.toMap(CoefficientPerQuantityParameter::getHour, CoefficientPerQuantityParameter::getCost));

        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map));
    }
}

上面的代码显示:

{
  "2" : 0.9,
  "40" : 0.7,
  "10" : 0.8
}

答案 1 :(得分:0)

另一种方法是使用th:inline="javascript"。您仍然必须将数据转换为Map<String, Double>才能获得所需的输出布局。例如:

控制器

List<CoefficientPerQuantityParameter> list = Arrays.asList(
        new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),
        new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),
        new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7))
);

Map<String, BigDecimal> map = list.stream().collect(Collectors.toMap(
        o -> "" + o.getHour(),
        CoefficientPerQuantityParameter::getCost)
);

模板

<span th:inline="javascript">[[${map}]]</span>

输出

<span>{"2":0.9,"40":0.7,"10":0.8}</span>