Translate Pojo to JSON

问题: I do the documentation. In pdf, my object should look like json. I create a collection of objects: Arrays.asList(new CoefficientPerQuantityParameter(2, BigDecimal.valueOf...

问题:

I do the documentation. In pdf, my object should look like json. I create a collection of objects:

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

CoefficientPerQuantityParameter looks like this

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

And I accept it in the xhtml file:

<?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>

I need to see the result in the form of JSON. But I see something completely different:

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

How to get it?

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

回答1:

Most classical way to serialise above list of POJO instances is to serialise it as array.

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));
    }
}

Above code prints:

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

If you want to have a structure where hour is a key and cost is a value I suggest to convert given array to Map manually and serialise result.

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));
    }
}

Above code prints:

{
  "2" : 0.9,
  "40" : 0.7,
  "10" : 0.8
}
  • 发表于 2019-02-13 22:39
  • 阅读 ( 318 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除