我们有一个类[[Agent,其中assignedUsers
为List << [Long] >>]当我们尝试将对象转换为JSON文档时,我们使用的是ObjectMapper
writeValueAsString
方法,该方法不会将id序列化为String,而JSON字符串中不会丢失键assignedUsers。 import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Agent {
private List<Long> assignedUserIds;
private String name;
private static final String json = "{\"name\":\"New Agency\", \"assignedUserIds\":[23,24]}";
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<Long> gocuetAssignedUserIds() {
return assignedUserIds;
}
public void setAssignedUserIds(final List<Long> assignedUserIds) {
this.assignedUserIds = assignedUserIds;
}
public static void main(String[] args) {
Agent agencyInfo = null;
try {
agencyInfo = new ObjectMapper().readValue(json, Agent.class);
System.out.println("Built Agent :: " + new ObjectMapper().writeValueAsString(agencyInfo)); // Outputs: Built Agent :: {"name":"New Agency"}
} catch (IOException e) {
e.printStackTrace();
}
}
}
[当我们尝试将对象转换为JSON文档时,我们正在使用ObjectMapper writeValueAsString方法,该方法不会序列化ID ...