如何使用 Java 中的 Jackson 将地图中的逗号分隔字符串转换为对象中的集合
How to convert comma separated string in map to Set in object using Jackson in Java
我是运行下一个节目:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test {
private static class Shape {
private String name;
private Set<String> colors;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getColors() {
return colors;
}
public void setColors(Set<String> colors) {
this.colors = colors;
}
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> attributes = new HashMap<>();
attributes.put("name", "table");
attributes.put("colors", "blue,green,red,black");
Shape shape = objectMapper.convertValue(attributes, Shape.class);
}
}
这里是pom.xml中的依赖:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.3</version>
</dependency>
</dependencies>
我遇到下一个错误:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot deserialize instance of `java.util.HashSet` out of VALUE_STRING token
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.company.test.Test$Shape["colors"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3751)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3669)
at com.company.test.Test.main(Test.java:36)
我试过改成:
attributes.put("colors", "[blue,green,red,black]");
AND
attributes.put("colors", "[\"blue\",\"green\",\"red\",\"black\"]");
但是没有用。解决方法可以是下一个:
...
Set<String> colors = new HashSet<>();
colors.add("blue");
colors.add("green");
colors.add("red");
colors.add("black");
attributes.put("colors", colors);
...
但是,当前实施不允许该解决方案。您是否想象过如何使用不同的方法来实施?
您可以使用 jackson-dataformats-text 库中的 CsvMapper
。还有,需要先把String
反序列化成Set<String>
,建一个Map
最后再转成Shape
:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CsvApp {
public static void main(String[] args) throws JsonProcessingException {
CsvMapper mapper = new CsvMapper();
String array = "blue,green,red,black";
Map<String, Object> attributes = new HashMap<>();
attributes.put("name", "table");
attributes.put("colors", mapper.readValue(array, new TypeReference<Set<String>>() {}));
Shape shape = mapper.convertValue(attributes, Shape.class);
System.out.println(shape);
}
}
我是运行下一个节目:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test {
private static class Shape {
private String name;
private Set<String> colors;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getColors() {
return colors;
}
public void setColors(Set<String> colors) {
this.colors = colors;
}
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> attributes = new HashMap<>();
attributes.put("name", "table");
attributes.put("colors", "blue,green,red,black");
Shape shape = objectMapper.convertValue(attributes, Shape.class);
}
}
这里是pom.xml中的依赖:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.3</version>
</dependency>
</dependencies>
我遇到下一个错误:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot deserialize instance of `java.util.HashSet` out of VALUE_STRING token
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.company.test.Test$Shape["colors"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3751)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3669)
at com.company.test.Test.main(Test.java:36)
我试过改成:
attributes.put("colors", "[blue,green,red,black]");
AND
attributes.put("colors", "[\"blue\",\"green\",\"red\",\"black\"]");
但是没有用。解决方法可以是下一个:
...
Set<String> colors = new HashSet<>();
colors.add("blue");
colors.add("green");
colors.add("red");
colors.add("black");
attributes.put("colors", colors);
...
但是,当前实施不允许该解决方案。您是否想象过如何使用不同的方法来实施?
您可以使用 jackson-dataformats-text 库中的 CsvMapper
。还有,需要先把String
反序列化成Set<String>
,建一个Map
最后再转成Shape
:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CsvApp {
public static void main(String[] args) throws JsonProcessingException {
CsvMapper mapper = new CsvMapper();
String array = "blue,green,red,black";
Map<String, Object> attributes = new HashMap<>();
attributes.put("name", "table");
attributes.put("colors", mapper.readValue(array, new TypeReference<Set<String>>() {}));
Shape shape = mapper.convertValue(attributes, Shape.class);
System.out.println(shape);
}
}