我在尝试从 json 创建模型时遇到问题,该模型由具有相同属性但名称不同的 json 数组组成。
这是我想将其转换为模型的 json 示例:
{
"AA": [
{
"t": 1605589200,
"o": 17.37,
"h": 18.3,
"l": 17.11,
"c": 18.28,
"v": 9578592
},
{
"t": 1605675600,
"o": 18.3,
"h": 18.85,
"l": 18.3,
"c": 18.575,
"v": 9092559
}
],
"AAIC": [
{
"t": 1605589200,
"o": 2.92,
"h": 3.045,
"l": 2.92,
"c": 3.02,
"v": 550468
},
{
"t": 1605675600,
"o": 3.11,
"h": 3.1684,
"l": 3.03,
"c": 3.05,
"v": 476259
}
]
}
请注意,每个数组都具有相同的对象结构,但具有不同的名称(“AA”和“AAIC”)。 我已经编写了以下代码:
@Data
public class SomeClass {
private List<SomeClassInfo> someClassInfo;
@Data
public class SomeClassInfo {
@JsonProperty(value = "t")
private Integer time;
@JsonProperty(value = "o")
private Double o;
@JsonProperty(value = "h")
private Double h;
@JsonProperty(value = "l")
private Double l;
@JsonProperty(value = "c")
private Double c;
@JsonProperty(value = "v")
private Integer v;
}
}
但这还不够,因为我不想制作 n 个具有特定名称的列表,例如
@JsonProperty(value = "AA")
List<SomeClassInfo> AA;
@JsonProperty(value = "AAIC")
List<SomeClassInfo> AAIC;
因为上面的示例代表 10k 数组中的 2 个,每个数组包含 n 个具有相同结构的 json 对象。
最佳答案
您可以使用ObjectMapper将您的json包装到
Map<String,List<SomeClassInfo>>
就这样
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String jsonInput = "{\"AA\":[{\"t\":1605589200,\"o\":17.37,\"h\":18.3,\"l\":17.11,\"c\":18.28,\"v\":9578592},{\"t\":1605675600,\"o\":18.3,\"h\":18.85,\"l\":18.3,\"c\":18.575,\"v\":9092559}],\"AAIC\":[{\"t\":1605589200,\"o\":2.92,\"h\":3.045,\"l\":2.92,\"c\":3.02,\"v\":550468},{\"t\":1605675600,\"o\":3.11,\"h\":3.1684,\"l\":3.03,\"c\":3.05,\"v\":476259}]}";
TypeReference<HashMap<String, List<SomeClassInfo>>> typeRef
= new TypeReference<HashMap<String, List<SomeClassInfo>>>() {
};
Map<String, List<SomeClassInfo>> map = null;
try {
map = mapper.readValue(jsonInput, typeRef);
System.out.println(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
@Getter
@Setter
@ToString
@Data
public static class SomeClassInfo {
@JsonProperty(value = "t")
private Integer time;
@JsonProperty(value = "o")
private Double o;
@JsonProperty(value = "h")
private Double h;
@JsonProperty(value = "l")
private Double l;
@JsonProperty(value = "c")
private Double c;
@JsonProperty(value = "v")
private Integer v;
}
}
响应将是这样的:
{AA=[test.SomeClassInfo(time=1605589200, o=17.37, h=18.3, l=17.11, c=18.28, v=9578592), test.SomeClassInfo(time=1605675600, o=18.3, h=18.85, l=18.3, c=18.575, v=9092559)], AAIC=[test.SomeClassInfo(time=1605589200, o=2.92, h=3.045, l=2.92, c=3.02, v=550468), test.SomeClassInfo(time=1605675600, o=3.11, h=3.1684, l=3.03, c=3.05, v=476259)]}
关于java - 在 Json 中映射具有公共(public)属性和动态名称的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64901095/