问题:
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
}