java—在json中Map具有公共属性和动态名称的数组

xzv2uavs  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(348)

我在尝试从json创建模型时遇到了一个问题,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对象。

k10s72fa

k10s72fa1#

我通过合并@hamza slama solution和我最终在以下链接找到的其他解决方案找到了解决方案:使用jackson和动态字段名反序列化json
我编辑的模型是:

@Data
public class SomeClass {

    private Map<String, SomeClassInfo[]> someClassInfoMap = new HashMap<>();

    @JsonAnySetter
    public void setSomeClassInfo (String name, SomeClassInfo[] someClassInfo) {
        someClassInfoMap.put(name, someClassInfo);
    }

    @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;
    }

}
axzmvihb

axzmvihb2#

可以使用objectmapper将json Package 到

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)]}

相关问题