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

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

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

问题

public class MyClass {
  private String name;
  private Double number;
}
英文:

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:

{
  "name": {"raw": "Hello world"},
  "number": {"raw": 7.5}
}

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:

public class MyClass {
  private String name;
  private Double number;
}

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):

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

答案1

得分: 1

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

class MyClass {
    private String name;
    private Double number;

    @JsonProperty("name")
    private void unpackNameFromNestedRawObject(Map<String, String> name) {
      this.name = name.get("raw");
    }

    @JsonProperty("number")
    private void unpackNumberFromNestedRawObject(Map<String, Double> number) {
      this.number = number.get("raw");
    }

    public String getName() {
      return name;
    }

    public void setName(final String name) {
      this.name = name;
    }

    public Double getNumber() {
      return number;
    }

    public void setNumber(final Double number) {
      this.number = number;
    }

    @Override
    public String toString() {
      return "MyClass{" + "name='" + name + '\'' + ", number=" + number + '}';
    }
}
英文:

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:

class MyClass {
    private String name;
    private Double number;

    @JsonProperty(&quot;name&quot;)
    private void unpackNameFromNestedRawObject(Map&lt;String, String&gt; name) {
      this.name = name.get(&quot;raw&quot;);
    }

    @JsonProperty(&quot;number&quot;)
    private void unpackNumberFromNestedRawObject(Map&lt;String, Double&gt; number) {
      this.number = number.get(&quot;raw&quot;);
    }

    public String getName() {
      return name;
    }

    public void setName(final String name) {
      this.name = name;
    }

    public Double getNumber() {
      return number;
    }

    public void setNumber(final Double number) {
      this.number = number;
    }

    @Override
    public String toString() {
      return &quot;MyClass{&quot; + &quot;name=&#39;&quot; + name + &#39;\&#39;&#39; + &quot;, number=&quot; + number + &#39;}&#39;;
    }
  }

答案2

得分: 0

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

public class Wrapper<T> {
    T value;
    // getters/setters
}

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

public class MyClass {
    Wrapper<String> aStringField;
    Wrapper<Integer> aIntegerField;
    ...
}

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

英文:

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

public class Wrapper&lt;T&gt; {
    T value;
    // getters/setters
}

And then I created my class like this one:

public class MyClass {
    RawWrapper&lt;String&gt; aStringField;
    RawWrapper&lt;Integer&gt; aIntegerField;
    ...
}

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:

确定