{ "updatorCode": "00013838", "updTm": 1694762024000, "checkTools": "OldApp" }此json转为java对象如何处理,其中updTm为Date类型
时间: 2023-11-09 19:05:17 浏览: 69
可以使用Jackson库将该json转为java对象,其中updTm需要使用@JsonFormat注解指定日期格式。具体代码如下:
```
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Date;
public class JsonToObject {
public static void main(String[] args) throws Exception {
String json = "{\"updatorCode\": \"00013838\", \"updTm\": 1694762024000, \"checkTools\": \"OldApp\"}";
ObjectMapper objectMapper = new ObjectMapper();
MyObject myObject = objectMapper.readValue(json, MyObject.class);
System.out.println(myObject);
}
static class MyObject {
@JsonProperty("updatorCode")
private String updatorCode;
@JsonProperty("updTm")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updTm;
@JsonProperty("checkTools")
private String checkTools;
// getter and setter
}
}
```
阅读全文