简体   繁体   English

使用Jackson将Json子对象转换为HashMap

[英]Convert Json subobject to a HashMap with Jackson

I would like to convert the bpi field of the following json string: 我想转换以下json字符串的bpi字段:

{
    "bpi": {
        "2017-10-15": 5697.3917,
        "2017-10-16": 5754.2213,
        "2017-10-17": 5595.235,
        "2017-10-18": 5572.1988,
        "2017-10-19": 5699.5838,
        "2017-10-20": 5984.0863,
        "2017-10-21": 6013.2288,
        "2017-10-22": 5984.9563,
        "2017-10-23": 5895.2988,
        "2017-10-24": 5518.85,
        "2017-10-25": 5733.9038,
        "2017-10-26": 5888.145,
        "2017-10-27": 5767.68,
        "2017-10-28": 5732.825,
        "2017-10-29": 6140.5313,
        "2017-10-30": 6121.8,
        "2017-10-31": 6447.6675
    },
        "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index. BPI value data returned as USD.",
        "time": {
        "updated": "Nov 1, 2017 13:49:57 UTC",
        "updatedISO": "2017-11-01T13:49:57+00:00"
    }
}

into a hashmap - preferably Map<Date, Double> but Map<String, String> will do as well (then I would have to process it later on). 到一个hashmap中-最好是Map<Date, Double>但是Map<String, String>也可以(然后我必须稍后对其进行处理)。 I read somewhere how to convert json arrays to java arrays but since the desired array is wrapped in the bpi sub field I dont know how to access it. 我在某处读过如何将json数组转换为java数组,但是由于所需的数组包装在bpi子字段中,因此我不知道如何访问它。 Could you please help me with that? 你能帮我吗?

Thanks a lot 非常感谢

It's pretty straight forward with Jackson. 与杰克逊很直接。 First define a class to hold the values: 首先定义一个类来保存值:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {

    private Map<LocalDate, Double> bpi;

    // Getters and setters ommited
}

Then read your JSON string: 然后读取您的JSON字符串:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

Foo foo = mapper.readValue(json, Foo.class);

Alternatively you can use: 或者,您可以使用:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

JsonNode node = mapper.readTree(json).get("bpi");

ObjectReader reader = mapper.readerFor(new TypeReference<Map<LocalDate, Double>>() {});
Map<LocalDate, Double> bpi = reader.readValue(node);

The LocalDate support comes from the jackson-datatype-jsr310 module. LocalDate支持来自jackson-datatype-jsr310模块。

Add the following dependency you project: 添加您所投影的以下依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jackson.version}</version>
</dependency>

And then register the JavaTimeModule in your ObjectMapper instance: 然后在您的ObjectMapper实例中注册JavaTimeModule

mapper.registerModule(new JavaTimeModule());

If you prefer to use Date instead of LocalDate , then you don't need any to add any dependency and you don't need to register any module either. 如果您更喜欢使用Date而不是LocalDate ,则不需要添加任何依赖项,也不需要注册任何模块。 Just use Date instead of LocalDate in the above examples. 在上面的示例中,只需使用Date而不是LocalDate However I advise you to use LocalDate instead. 但是我建议您改用LocalDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM