亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Pojo 轉換為 JSON

將 Pojo 轉換為 JSON

jeck貓 2022-06-15 17:25:14
我做文檔。在 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 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

序列化上述實例列表的最經典方法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));

    }

}

上面的代碼打?。?/p>


[ {

  "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

}


查看完整回答
反對 回復 2022-06-15
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

另一種方法是使用th:inline="javascript". 您仍然需要將數據轉換為 aMap<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>


查看完整回答
反對 回復 2022-06-15
  • 2 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號