将一个 JSON 占位符文件映射到 Java 对象

huangapple go评论83阅读模式
英文:

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

   {
  &quot;class&quot;: [
    {
      &quot;id&quot;: &quot;default&quot;,
      &quot;type&quot;: [
        &quot;riskType&quot;,
        &quot;firstTime&quot;
      ],
      &quot;direction&quot;: [],
      &quot;town&quot;: [
        &quot;leader&quot;,
        {
          &quot;streets&quot;: [
            &quot;mainStreet&quot;
          ]
        }
      ]
    }
  ]
}

To map it I am using a
ojectMapper.readValue(inputStream, new TypeReference&lt;HashMap&lt;String, Set&lt;PayloadClass&gt;&gt;&gt;() {
});

My payload class is as follows:

@Data
@EqualsAndHashCode(exclude = {&quot;town&quot;, &quot;direction&quot;, &quot;type&quot;})
@NoArgsConstructor
public class PayloadClass {
private String id;
@NonNull private Set&lt;String&gt; type = Collections.emptySet();
@NonNull private Set&lt;String&gt; direction = Collections.emptySet();
@NonNull private Set&lt;Town&gt; 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 = &quot;leader&quot;)
private String leader;

@JsonProperty(value = &quot;streets&quot;)
private Set&lt;String&gt; 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 
(&#39;leader&#39;)
at [Source: (BufferedInputStream); line: 16, column: 9] (through reference chain: 
java.util.HashMap[&quot;PayloadClass&quot;]-&gt;java.util.HashSet[0]- 
com.PayloadClass[&quot;town&quot;]-&gt;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 :

{
  &quot;town&quot;: [
    {
      &quot;leader&quot;: &quot;&quot;,
      &quot;streets&quot; :  [
        &quot;mainStreet&quot;
      ]
    }
  ]
}

huangapple
  • 本文由 发表于 2020年10月2日 16:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64168538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定