英文:
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("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 + '}';
}
}
答案2
得分: 0
对于有相同疑问的任何人,我最终创建了一个类似这样的类:
public class Wrapper<T> {
T value;
// getters/setters
}
然后我创建了我的类,类似这样:
public class MyClass {
Wrapper<String> aStringField;
Wrapper<Integer> aIntegerField;
...
}
有了这个,JSON 被正确解析。如果你想将其转换为另一个没有 RawWrappers 但具有相同字段的类,我在这种情况下使用了 Mapstruct 来指示如何从 RawWrapper<String>
转换为 String
,RawWrapper<Integer>
转换为 Integer
,等等,就是这样!
英文:
For anyone who have the same doubt, I finally created a class like this one:
public class Wrapper<T> {
T value;
// getters/setters
}
And then I created my class like this one:
public class MyClass {
RawWrapper<String> aStringField;
RawWrapper<Integer> 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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论