简体   繁体   English

Jackson读取JSON并转换为Map <String,Object>

[英]Jackson read JSON and convert to Map<String,Object>

I have a JSON where one of the values is ISO-8601 datetime string. 我有一个JSON,其中一个值是ISO-8601日期时间字符串。 I need to convert it to java.util.Date. 我需要将其转换为java.util.Date。 How do I do it? 我该怎么做?

I'm not sure if the following code is correct. 我不确定以下代码是否正确。

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Map;

public class CustomObjectMapper extends ObjectMapper {

    private static final long serialVersionUID = 2686298573056737140L;
    private static final SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" );


    public CustomObjectMapper() {
        super.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        super.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        super.setDateFormat(df);
    }

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new CustomObjectMapper();
        Map<String, Object> map  = mapper.readValue(JSON, new TypeReference<Map<String, Object>>(){});
        System.out.println(map);
        for(String key: map.keySet()) {
            Object val = map.get(key);
            System.out.println("keyName is " + key + " value is " + val + " value type is " + val.getClass());

        }
    }


    private static final String JSON = "{\"mybool\":true,\"mydate\":\"2016-02-11T18:30:00.511-08:00\",\"mystring\":\"test\",\"myint\":1234}";


}

When I print the map content, I see that "mydate" value is showing up as String. 当我打印地图内容时,我看到“mydate”值显示为String。

key is mybool value is true value type is class java.lang.Boolean
key is mydate value is 2016-02-11T18:30:00.511-08:00 value type is class java.lang.String
key is mystring value is test value type is class java.lang.String
key is myint value is 1234 value type is class java.lang.Integer

Can I configure Jackson where for the key "mydate" the value type has to java.util.Date? 我可以为Jackson配置值类型对java.util.Date的密钥“mydate”吗?

Assuming that you know the JSON structure then you can have a POJO to map its properties and use @JsonFormat annotation (available from Jackson 2.0) as follows: 假设您知道JSON结构,那么您可以使用POJO映射其属性并使用@JsonFormat注释(可从Jackson 2.0获得),如下所示:

import com.fasterxml.jackson.annotation.JsonFormat

class MyPojo {

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    Date mydate

    String mystring
    Boolean mybool
    Integer myint    
}

Then on your mapper class: 然后在mapper类上:

MyPojo myPojo = mapper.readValue(JSON, MyPojo.class);

No you cannot. 你不能。 JSON format does not have Date data type. JSON格式没有Date数据类型。 It can be only String. 它只能是String。 So you have to parse it to java.util.Date on your own. 所以你必须自己解析它到java.util.Date。 As example by using: 例如使用:

new java.util.Date(javax.xml.bind.DatatypeConverter.parseDateTime(val).getTimeInMillis());

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

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