在Java中解析带有中间元素的JSON({“realKey”: {“badKey”: “goodValue”}})

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

Parse JSON in Java with intermediate element ({"realKey": {"badKey": "goodValue"}}))

问题

  1. public class MyClass {
  2. private String name;
  3. private Double number;
  4. }
英文:

I have a problem parsing a JSON to a Java class. I have read about Elasticsearch Search App APIs, and I have found responses are like this one:

  1. {
  2. "name": {"raw": "Hello world"},
  3. "number": {"raw": 7.5}
  4. }

That is, every field is an object with a key called raw and the real value I want. I would like to have a Java class like this one:

  1. public class MyClass {
  2. private String name;
  3. private Double number;
  4. }

I haven't seen in Jackson or any other mapping library something in order to map this JSON easily.

I would expect something like that if we use Jackson (is not a requisite, we could use any other parsing library):

  1. ObjectMapper objectMapper = new ObjectMapper();
  2. MyClass jsonParsed = objectMapper.readValue(json, MyClass.class);

答案1

得分: 1

你可以将 JSON 属性视为 Map 并解压所需的属性并赋值给相应的属性。你应该能够做类似以下的事情:

  1. class MyClass {
  2. private String name;
  3. private Double number;
  4. @JsonProperty("name")
  5. private void unpackNameFromNestedRawObject(Map<String, String> name) {
  6. this.name = name.get("raw");
  7. }
  8. @JsonProperty("number")
  9. private void unpackNumberFromNestedRawObject(Map<String, Double> number) {
  10. this.number = number.get("raw");
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(final String name) {
  16. this.name = name;
  17. }
  18. public Double getNumber() {
  19. return number;
  20. }
  21. public void setNumber(final Double number) {
  22. this.number = number;
  23. }
  24. @Override
  25. public String toString() {
  26. return "MyClass{" + "name='" + name + '\'' + ", number=" + number + '}';
  27. }
  28. }
英文:

You can read the JSON Property as Map and unpack the needed attribute and assign to the property. You should be able to do something like:

  1. class MyClass {
  2. private String name;
  3. private Double number;
  4. @JsonProperty(&quot;name&quot;)
  5. private void unpackNameFromNestedRawObject(Map&lt;String, String&gt; name) {
  6. this.name = name.get(&quot;raw&quot;);
  7. }
  8. @JsonProperty(&quot;number&quot;)
  9. private void unpackNumberFromNestedRawObject(Map&lt;String, Double&gt; number) {
  10. this.number = number.get(&quot;raw&quot;);
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(final String name) {
  16. this.name = name;
  17. }
  18. public Double getNumber() {
  19. return number;
  20. }
  21. public void setNumber(final Double number) {
  22. this.number = number;
  23. }
  24. @Override
  25. public String toString() {
  26. return &quot;MyClass{&quot; + &quot;name=&#39;&quot; + name + &#39;\&#39;&#39; + &quot;, number=&quot; + number + &#39;}&#39;;
  27. }
  28. }

答案2

得分: 0

对于有相同疑问的任何人,我最终创建了一个类似这样的类:

  1. public class Wrapper<T> {
  2. T value;
  3. // getters/setters
  4. }

然后我创建了我的类,类似这样:

  1. public class MyClass {
  2. Wrapper<String> aStringField;
  3. Wrapper<Integer> aIntegerField;
  4. ...
  5. }

有了这个,JSON 被正确解析。如果你想将其转换为另一个没有 RawWrappers 但具有相同字段的类,我在这种情况下使用了 Mapstruct 来指示如何从 RawWrapper<String> 转换为 StringRawWrapper<Integer> 转换为 Integer,等等,就是这样!

英文:

For anyone who have the same doubt, I finally created a class like this one:

  1. public class Wrapper&lt;T&gt; {
  2. T value;
  3. // getters/setters
  4. }

And then I created my class like this one:

  1. public class MyClass {
  2. RawWrapper&lt;String&gt; aStringField;
  3. RawWrapper&lt;Integer&gt; aIntegerField;
  4. ...
  5. }

With that, JSON is parsed correctly. If then you want to transform it to another class with the same fields but without RawWrappers, in my case I have used Mapstruct to indicate how to pass from RawWrapper<String> to String, RawWrapper<Integer> to Integer, etc., and that's all!

huangapple
  • 本文由 发表于 2023年4月6日 19:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948745.html
匿名

发表评论

匿名网友

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

确定