我该如何转换它:
"parameters" : [ "{name:\"xx\", value:\"45\",typRef:\"FE\",unit:\"grad\"}" ],
改为:
"parameters" : [ {
"name" : "xx",
"value" : "45",
"typRef" : "FE",
"unit" : "grad"
}]
我使用objectMapper来写字符串
public class ParameterClass
{
@NonNull private final String name;
@JsonInclude(JsonInclude.Include.NON_NULL) private final String value;
@JsonInclude(JsonInclude.Include.NON_NULL) private final typRef;
@JsonInclude(JsonInclude.Include.NON_NULL) private final String unit;
private ParameterClass (@NonNull String name, String value, String typRef, String unit)
{
this.name = name;
this.value = value;
this.typRef = typRef;
this.unit = unit;
}
构建JSON的方法
buildAsJson(String name, String unit, ...) {
ParameterClass parameter = new ParameterClass(name, unit, ..);
return objectMapper.writeValueAsString(parameter);
}
我把所有的都列在一个清单上:
List<String> parameters = List.of(buildAsJson("xx", "45", ...), buildAsJson(...))
1条答案
按热度按时间euoag5mw1#
只需存储字符串
parameter
,然后用objectMapper.readTree(...)
读取该JSON字符串,如果JSON字符串有效,它会将该JSON字符串转换为有效的JSON格式。