![](/img/trans.png)
[英]How to convert a JSONObject to a Java Bean with string variables?
[英]JSONObject to Java bean class not working
我已經在jersy client
編寫了一個Web jersy client
,並將json字符串轉換為bean類,但是日期顯示為error。 我嘗試了@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
但仍然在方法中給出了相同的錯誤。 如何在我的代碼中將字符串轉換為日期格式。
錯誤:
not a valid representation (error: Failed to parse Date value 'Aug 7, 2017 1:35:00 PM': Can not parse date "Aug 7, 2017 1:35:00 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
網絡服務
@POST
@Path("/driverLocation")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response handleDriverLocation(String jsonRequest) {
List<DriverLocationDetails> driverLocationDetails = bookingService
.getLocationDetailsFromDB();
boolean save = false;
for (DriverLocationDetails details : driverLocationDetails) {
Gson g = new Gson();
jsonRequest = g.toJson(details);
DriverLocationDetails driverLocationDetail = TmsUtil
.readAsObjectOf(DriverLocationDetails.class, jsonRequest);
save = bookingService.saveLocation(driverLocationDetail);
}
JSONObject jo = new JSONObject();
try {
if (save) {
jo.put("Save", "saved");
} else {
jo.put("Save", "failed");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Response.status(200).entity(jo.toString()).build();
}
static com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
public static <T> T readAsObjectOf(Class<T> clazz, String value) {
try {
return MAPPER.readValue(value, clazz);
} catch (Exception e) {
logger.error("{}, {}", e.getMessage(), e.fillInStackTrace());
}
return null;
}
安慰
Can not construct instance of java.util.Date from String value 'Aug 7, 2017 1:35:00 PM': not a valid representation (error: Failed to parse Date value 'Aug 7, 2017 1:35:00 PM': Can not parse date "Aug 7, 2017 1:35:00 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: java.io.StringReader@4d768e3c; line: 1, column: 72] (through reference chain: in.greenorange.model.DriverLocationDetails["gpsLocationTime"]), com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value 'Aug 7, 2017 1:35:00 PM': not a valid representation (error: Failed to parse Date value 'Aug 7, 2017 1:35:00 PM': Can not parse date "Aug 7, 2017 1:35:00 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: java.io.StringReader@4d768e3c; line: 1, column: 72] (through reference chain: in.greenorange.model.DriverLocationDetails["gpsLocationTime"])
您的模式必須如下所示:
@JsonFormat(pattern = "MMM d',' yyyy HH:mm:ss a")
因為這是您從服務器獲得的模式
我只是用GSon進行轉換,因為傑克遜有轉換
DriverLocationDetails driverLocationDetail = g.fromJson(jsonRequest, DriverLocationDetails.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.