将 Pojo 转换为 JSON

我做文档。在 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} 


jeck猫
浏览 142回答 2
2回答

繁花不似锦

序列化上述实例列表的最经典方法POJO是将其序列化为数组。import com.fasterxml.jackson.databind.ObjectMapper;import java.math.BigDecimal;import java.util.Arrays;import java.util.List;public class MapperApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; List<CoefficientPerQuantityParameter> coefficients = Arrays.asList(new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7)));&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(coefficients));&nbsp; &nbsp; }}上面的代码打印:[ {&nbsp; "hour" : 2,&nbsp; "cost" : 0.9}, {&nbsp; "hour" : 10,&nbsp; "cost" : 0.8}, {&nbsp; "hour" : 40,&nbsp; "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 {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; List<CoefficientPerQuantityParameter> coefficients = Arrays.asList(new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7)));&nbsp; &nbsp; &nbsp; &nbsp; Map<Integer, BigDecimal> map = coefficients.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(CoefficientPerQuantityParameter::getHour, CoefficientPerQuantityParameter::getCost));&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map));&nbsp; &nbsp; }}上面的代码打印:{&nbsp; "2" : 0.9,&nbsp; "40" : 0.7,&nbsp; "10" : 0.8}

青春有我

另一种方法是使用th:inline="javascript". 您仍然需要将数据转换为 aMap<String, Double>以获得所需的输出布局。例如:控制器List<CoefficientPerQuantityParameter> list = Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(2, BigDecimal.valueOf(0.9)),&nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(10, BigDecimal.valueOf(0.8)),&nbsp; &nbsp; &nbsp; &nbsp; new CoefficientPerQuantityParameter(40, BigDecimal.valueOf(0.7)));Map<String, BigDecimal> map = list.stream().collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp; o -> "" + o.getHour(),&nbsp; &nbsp; &nbsp; &nbsp; CoefficientPerQuantityParameter::getCost));模板<span th:inline="javascript">[[${map}]]</span>输出<span>{"2":0.9,"40":0.7,"10":0.8}</span>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java