英文:
How to deserialize objects with dynamic name
问题
以下是您要翻译的内容:
我想将以下JSON响应反序列化为Java对象。
但是字段Time Series的名称是动态的,例如可以是Time Series (5min)
,Time Series (1min)
,Time Series (15min)
等。
是否有任何注释或配置可以帮助将JSON反序列化为POJO,无论字段名称如何?
JSON响应:
{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "IBM",
"3. Last Refreshed": "2020-10-09 19:55:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2020-10-09 19:55:00": {
"1. open": "127.7500",
"2. high": "127.7500",
"3. low": "127.7000",
"4. close": "127.7000",
"5. volume": "405"
},
"2020-10-09 19:45:00": {
"1. open": "127.7600",
"2. high": "127.7600",
"3. low": "127.7600",
"4. close": "127.7600",
"5. volume": "200"
}
}
}
JSON响应的POJO类:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
private Map<String, IntraDayData> timeSeries;
}
英文:
I'm want to deserialize the below JSON response into a Java object.
But the name of field Time Series is dynamic for e.g. it can be Time Series (5min)
, Time Series (1min)
, Time Series (15min)
etc.
Is there any annotation or configuration that can help in deserializing the JSON into POJO irrespective of field name?
JSON Response:
{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "IBM",
"3. Last Refreshed": "2020-10-09 19:55:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2020-10-09 19:55:00": {
"1. open": "127.7500",
"2. high": "127.7500",
"3. low": "127.7000",
"4. close": "127.7000",
"5. volume": "405"
},
"2020-10-09 19:45:00": {
"1. open": "127.7600",
"2. high": "127.7600",
"3. low": "127.7600",
"4. close": "127.7600",
"5. volume": "200"
}
}
}
POJO for JSON response:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
private Map<String, IntraDayData> timeSeries;
}
答案1
得分: 0
由于元数据已经包含有关间隔的信息,您可以利用 JsonAlias - 就像这样。
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
@JsonAlias({ "Time Series (5min)", "Time Series (1min)", "Time Series (15min)" })
private Map<String, IntraDayData> timeSeries;
}
英文:
Since metadata already contains the information about the interval, you could utilise JsonAlias - something like this.
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
@JsonAlias({ "Time Series (5min)", "Time Series (1min)", "Time Series (15min)" })
private Map<String, IntraDayData> timeSeries;
}
答案2
得分: 0
我认为@JsonAnySetter适用于这种情况。基本上,被注解的字段(从版本2.8开始支持,如文档中所述)然后用作默认值来反序列化未知属性。请尝试这样做。
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
@JsonAnySetter
private Map<String, IntraDayData> timeSeries;
}
英文:
I believe @JsonAnySetter is suitable for this. Basically, the annotated field (support on field from version 2.8, as mentioned in doc) is then used as a default to deserialize unknown properties. Please try this.
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
@JsonAnySetter
private Map<String, IntraDayData> timeSeries;
}
答案3
得分: 0
问题通过创建一个带有String
键和Object
值作为参数的setter方法,并使用@JsonAnySetter
进行注释得以解决。
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
private Map<String, IntraDayData> timeSeries = new HashMap<>();
@JsonAnySetter
public void setTimeSeries(String key, Object value) {
this.timeSeries = (Map<String, IntraDayData>) value;
}
}
字符串键将具有类似于:"Time Series (5min)","Time Series (1min)","Time Series (15min)"
的值。
对象值将包含内容。
英文:
The issue got resolved by creating a setter method with String
key and Object
value as argument and annotating it with @JsonAnySetter
.
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class IntraDayResponse {
@JsonProperty("Meta Data")
private Metadata metadata;
private Map<String, IntraDayData> timeSeries = new HashMap<>();
@JsonAnySetter
public void setTimeSeries(String key, Object value) {
this.timeSeries = (Map<String, IntraDayData>) value;
}
}
String key would have values like: "Time Series (5min)", "Time Series (1min)", "Time Series (15min)"
Object value will have the content.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论