我的java pojo类是
public class Task implements java.io.Serializable {
private Integer id;
private Date startDate;
private Date endDate;
private Integer priority;
private Double duration;
private Integer percentDone;
private String durationUnit;
private Integer parentId;
private String taskName;
private Integer taskindex;
private Integer depth;
public Task() {
}
public Task(Date startDate, Date endDate, Integer priority,
Double duration, Integer percentDone, String durationUnit,
Integer parentId, String taskName, Integer taskindex, Integer depth) {
this.startDate = startDate;
this.endDate = endDate;
this.priority = priority;
this.duration = duration;
this.percentDone = percentDone;
this.durationUnit = durationUnit;
this.parentId = parentId;
this.taskName = taskName;
this.taskindex = taskindex;
this.depth = depth;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Temporal(TemporalType.DATE)
@Column(name = "StartDate", length = 10)
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "EndDate", length = 10)
public Date getEndDate() {
return this.endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
@Column(name = "Priority")
public Integer getPriority() {
return this.priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
@Column(name = "Duration", precision = 22, scale = 0)
public Double getDuration() {
return this.duration;
}
public void setDuration(Double duration) {
this.duration = duration;
}
@Column(name = "PercentDone")
public Integer getPercentDone() {
return this.percentDone;
}
public void setPercentDone(Integer percentDone) {
this.percentDone = percentDone;
}
@Column(name = "DurationUnit", length = 20)
public String getDurationUnit() {
return this.durationUnit;
}
public void setDurationUnit(String durationUnit) {
this.durationUnit = durationUnit;
}
@Column(name = "parentId")
public Integer getParentId() {
return this.parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
@Column(name = "TaskName")
public String getTaskName() {
return this.taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
@Column(name = "Taskindex")
public Integer getTaskindex() {
return this.taskindex;
}
public void setTaskindex(Integer taskindex) {
this.taskindex = taskindex;
}
@Column(name = "depth")
public Integer getDepth() {
return this.depth;
}
public void setDepth(Integer depth) {
this.depth = depth;
}
我的util将把我的jsonObject中的值赋给Pojo
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd'T'HH:mm:ss"}));
JSONObject jsonObject = JSONObject.fromObject(data);
System.out.println("JSON OBJECT VALUE FOR PARENTID ::"+jsonObject.get("parentId"));
jsonObject.put("taskName", jsonObject.getString("Name"));
jsonObject.put("startDate", jsonObject.get("StartDate"));
jsonObject.put("endDate", jsonObject.get("EndDate"));
jsonObject.put("duration", jsonObject.getInt("Duration"));
jsonObject.put("durationUnit", jsonObject.getString("DurationUnit"));
jsonObject.put("percentDone", jsonObject.getInt("PercentDone"));
jsonObject.put("priority", jsonObject.getInt("Priority"));
jsonObject.put("parentId", jsonObject.getInt("parentId"));
jsonObject.put("taskindex", jsonObject.getInt("index"));
jsonObject.put("depth", jsonObject.getInt("depth"));
jsonObject.remove("Id");
jsonObject.remove("ManuallyScheduled");
jsonObject.remove("checked");
jsonObject.remove("Name");
jsonObject.remove("StartDate");
jsonObject.remove("EndDate");
jsonObject.remove("Duration");
jsonObject.remove("DurationUnit");
jsonObject.remove("PercentDone");
jsonObject.remove("Priority");
jsonObject.remove("index");
System.out.println("JSON OBJECT ::"+jsonObject);
Task newTask = (Task) JSONObject.toBean(jsonObject, Task.class);
我收到的服务器端的jsonObject是
{
"parentId": 0,
"depth": 1,
"taskName": "New Task",
"startDate": "2012-01-30T00:00:00",
"endDate": "2012-01-31T00:00:00",
"duration": 1,
"durationUnit": "d",
"percentDone": 60,
"priority": 1,
"taskindex": 5 }
不知道为什么我无法将startDate分配给我的startDate POJO对象。
当我删除 JSONUtils.getMorpherRegistry()。registerMorpher(new DateMorpher(new String [] {“yyyy-MM-dd'T'HH:mm:ss”})); < / p>
将值分配给我的POJO对象,一切正常,但startDate更改为当前日期,然后分配。而且我正在向我的服务器端发出警告
WARNING: Can't transform property 'startDate' from java.lang.String into java.util.Date. Will register a default Morpher INFO: Property 'java.util.Date.class' has no write method. SKIPPED. WARNING: Property 'java.lang.String.date' does not exist. SKIPPED. INFO: Property 'java.util.Date.day' has no write method. SKIPPED. WARNING: Property 'java.lang.String.hours' does not exist. SKIPPED. WARNING: Property 'java.lang.String.minutes' does not exist. SKIPPED. WARNING: Property 'java.lang.String.month' does not exist. SKIPPED. WARNING: Property 'java.lang.String.seconds' does not exist. SKIPPED. INFO: Property 'java.util.Date.timezoneOffset' has no write method. SKIPPED.
在此之后,当我打印POJO赋值时,我将获得 startDate作为Thu Jan 12 10:09:27 IST 2012 和 endDate as Thu Jan 12 10:09:27 IST 2012
那么将jsonObject分配给我的Pojo Date对象是什么问题
答案 0 :(得分:1)
您的DateMorpher格式字符串不正确,请尝试以下操作:
yyyy-MM-dd'T'HH:mm:ss
了解更多信息,请参阅java文档:http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
答案 1 :(得分:0)
作为一种变通方法,您可以使用StringTokenizer和T作为分隔符,以获取Date部分,然后创建Date对象。
答案 2 :(得分:0)
Gson gson = new Gson(); YourEntityClass e_class = gson.fromJson(JsonData,YourEntityClass.class); 可以帮到你。
答案 3 :(得分:0)
仅供参考,在DateMorpher类中,您在参数中传递的日期被置为小写(我不明白为什么),所以真正的模式应该是“yyyy-MM-dd't'HH:mm:ss” 。希望这会有所帮助。