将Pojo转换为JSON

问题描述 投票:1回答:2

我做文件。在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))
)

每个数量系数参数如下所示

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} 
java jackson xhtml thymeleaf pojo
2个回答
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
}

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>
© www.soinside.com 2019 - 2024. All rights reserved.