List 在Java中未序列化为String

问题描述 投票:0回答:1

我们有一个类[[Agent,其中assignedUsersList << [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(); } } }

[我们有一个类Agent,其AssignedUsers为List
[当我们尝试将对象转换为JSON文档时,我们正在使用ObjectMapper writeValueAsString方法,该方法不会序列化ID ...
java serialization objectmapper
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.