【问题标题】:Translate Pojo to JSON将 Pojo 转换为 JSON
【发布时间】:2019-07-06 05:06:07
【问题描述】:

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

【问题讨论】:

    标签: java jackson xhtml thymeleaf pojo


    【解决方案1】:

    序列化上述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
    }
    

    【讨论】:

      【解决方案2】:

      另一种方法是使用th:inline="javascript"。您仍然需要将数据转换为 Map&lt;String, Double&gt; 以获得所需的输出布局。例如:

      控制器

      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-25
        • 2023-02-02
        • 2019-02-17
        • 1970-01-01
        相关资源
        最近更新 更多