英文:
Mapping a Json placeholder file to java Object
问题
以下是翻译好的内容:
我有以下的 JSON 数据来表示一个 City 对象,我想将其映射到 JAVA 中的一个对象。这用于过滤掉不需要的字段。
{
"class": [
{
"id": "default",
"type": ["riskType", "firstTime"],
"direction": [],
"town": [
"leader",
{
"streets": ["mainStreet"]
}
]
}
]
}
我使用以下代码来进行映射:
objectMapper.readValue(inputStream, new TypeReference<HashMap<String, Set<PayloadClass>>>() {});
我的 PayloadClass 类如下所示:
@Data
@EqualsAndHashCode(exclude = {"town", "direction", "type"})
@NoArgsConstructor
public class PayloadClass {
private String id;
@NonNull private Set<String> type = Collections.emptySet();
@NonNull private Set<String> direction = Collections.emptySet();
@NonNull private Set<Town> town = Collections.emptySet();
}
而我的 Town 类如下所示,用于表示一个城镇有一个领袖和一些街道:
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Town {
@JsonProperty(value = "leader")
private String leader;
@JsonProperty(value = "streets")
private Set<String> streets = Collections.emptySet();
}
问题出现在映射阶段,它试图在一个对象内映射领袖和街道的集合。
然后我遇到了以下错误,但不确定原因:
ERROR - 无法构造 `com.Town` 的实例(尽管至少存在一个创建者):没有从 String 值('leader')反序列化的 String-argument 构造函数/工厂方法
在 [Source: (BufferedInputStream); line: 16, column: 9] 处(通过引用链:java.util.HashMap["PayloadClass"]->java.util.HashSet[0]-com.PayloadClass["town"]->java.util.HashSet[1])
我试图保持 JSON 在这种格式以便上游应用程序使用,希望我可以解决映射类型中的这个问题。
因此,我正在尝试添加的新字段是 JSON 中的领袖字段。没有这个字段时,映射可以正常工作。
英文:
So I have the following JSON to represent a City object which I want to MAP to an object in JAVA.
This is used to filter out unwanted fields
{
"class": [
{
"id": "default",
"type": [
"riskType",
"firstTime"
],
"direction": [],
"town": [
"leader",
{
"streets": [
"mainStreet"
]
}
]
}
]
}
To map it I am using a
ojectMapper.readValue(inputStream, new TypeReference<HashMap<String, Set<PayloadClass>>>() {
});
My payload class is as follows:
@Data
@EqualsAndHashCode(exclude = {"town", "direction", "type"})
@NoArgsConstructor
public class PayloadClass {
private String id;
@NonNull private Set<String> type = Collections.emptySet();
@NonNull private Set<String> direction = Collections.emptySet();
@NonNull private Set<Town> town = Collections.emptySet();
And my town class is done as so to represent a a Town has a leader and streets
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Town{
@JsonProperty(value = "leader")
private String leader;
@JsonProperty(value = "streets")
private Set<String> streets = Collections.emptySet();
The issue is in the mapping, where it tries to map the leader and Set of streets within the one object.
I am then getting the following error but not sure why:
U [main] ERROR - Cannot construct instance of `
com.Town` (although at least one Creator
exists): no String-argument constructor/factory method to deserialize from String value
('leader')
at [Source: (BufferedInputStream); line: 16, column: 9] (through reference chain:
java.util.HashMap["PayloadClass"]->java.util.HashSet[0]-
com.PayloadClass["town"]->java.util.HashSet[1])
I am trying to keep the JSON in this format for upstream apps, hopefully I can resolve this issue in the mapping types.
So the new field I am trying to add, is the leader field within the Json. Without this field it performs without issue.
答案1
得分: 1
你的 JSON 格式有误。如果 town
是一个由具有领导者和街道名称数组的城市对象组成的数组,你应该在 JSON 中写入:
{
"town": [
{
"leader": "",
"streets": [
"mainStreet"
]
}
]
}
英文:
Your json format is wrong. If town is an array of city objects with a leader and an array of street names, you should write in json :
{
"town": [
{
"leader": "",
"streets" : [
"mainStreet"
]
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论