如何将多个JSON字段映射到一个Java Map?

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

How to map several json fields to a single java map?

问题

如何将此 JSON 映射到此 Java 对象,其中 habits Map 将具有键 HabitName.FITNESSHabitName.MATHEMATICS 以及相应的值?构造函数、getter 和 setter 方法已被省略以提高可读性。

public class IncomingJson {
  Map<HabitName, HabitDesc> habits;

  public enum HabitName {
    @JsonProperty("fitness") FITNESS,
    @JsonProperty("mathematics") MATHEMATICS
  }
  
  public static class HabitDesc {
    String duration;
    String score;
  }
}

运行时,我收到以下异常:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "fitness" (class IncomingJson), not marked as ignorable (one known property: "habits")

请注意,你提供的JSON中使用了HTML实体编码,所以在代码中应该使用普通的双引号 "fitness""mathematics" 而不是 &quot;fitness&quot;&quot;mathematics&quot;。如果你的JSON中确实包含了HTML实体编码,需要在反序列化时先将其解码为普通字符串。

英文:

How to map this JSON to this Java object where the habits Map will have the keys HabitName.FITNESS and HabitName.MATHEMATICS with the corresponding values? Constructor, getters and setters are omitted to readability.

{
  &quot;habits&quot;: {
    &quot;fitness&quot;: {
      &quot;duration&quot;: &quot;1 week&quot;,
      &quot;score&quot;: &quot;2.1&quot;
    },
    &quot;mathematics&quot;: {
      &quot;duration&quot;: &quot;2 weeks&quot;,
      &quot;score&quot;: &quot;2.4&quot;
    }
  }
}
public class IncomingJson {
  Map&lt;HabitName, HabitDesc&gt; habits;

  public enum HabitName {
    @JsonProperty(&quot;fitness&quot;) FITNESS,
    @JsonProperty(&quot;mathematics&quot;) MATHEMATICS
  }
  
  public static class HabitDesc {
    String duration;
    String score;
  }
}

At runtime I get the following Exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field &quot;fitness&quot; (class IncomingJson), not marked as ignorable (one known property: &quot;habits&quot;) 

答案1

得分: 0

解决方案已经接近了。您可以轻松将此JSON反序列化为以下Java类。

@Getter
@RequiredArgsConstructor
@JsonRootName("root")
public class Habits {
    private final Map<HabitName, HabitDesc> habits = new HashMap<>();
    
    public enum HabitName {
        @JsonProperty("fitness") FITNESS,
        @JsonProperty("mathematics") MATHEMATICS
    }
    
    @Getter
    @RequiredArgsConstructor
    public static class HabitDesc {
        @JsonProperty("duration") private final String duration;
        @JsonProperty("score") private final String score;
    }
}

重要提示: 上述类依赖于lombok.config文件中的lombok.anyconstructor.addconstructorproperties=true,该文件位于与Habits.class相同的包中。

因此,您的反序列化代码将如下所示:

var objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Habits habits = objectMapper.readValue(JSON_STRING, Habits.class);
英文:

The solution was close. You can deserialize this JSON into the following Java class easily.

{
	&quot;root&quot; : {
		&quot;habits&quot;: {
			&quot;fitness&quot;: {
				&quot;duration&quot;: &quot;1 week&quot;,
				&quot;score&quot;: &quot;2.1&quot;
			},
			&quot;mathematics&quot;: {
				&quot;duration&quot;: &quot;2 weeks&quot;,
				&quot;score&quot;: &quot;2.4&quot;
			}
		}
	}
}
@Getter
@RequiredArgsConstructor
@JsonRootName(&quot;root&quot;)
publi class Habits {
	private final Map&lt;HabitName, HabitDesc&gt; habits = new HashMap&lt;&gt;();
	public enum HabitName {
		@JsonProperty(&quot;fitness&quot;) FITNESS,
		@JsonProperty(&quot;mathematics&quot;) MATHEMATICS
	}
	@Getter
	@RequiredArgsConstructor
	public static class HabitDesc {
		@JsonProperty(&quot;duration&quot;) private final String duration;
		@JsonProperty(&quot;score&quot;) private final String score;
	}
}

IMPORTANT: The above class relies on lombok.anyconstructor.addconstructorproperties=true in the lombok.config file in the same package as Habits.class.

So your deserialization code will look like this

var objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Habits = objectMapper.readValue(JSON_STRING, Habits.class);

huangapple
  • 本文由 发表于 2020年8月8日 17:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63313983.html
匿名

发表评论

匿名网友

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

确定