[英]HttpMessageNotReadableException: Could not read JSON: Failed to parse Date value 'I'
The following is the code,json file and the exception I am getting while executing the code. 以下是代码,json文件和执行代码时遇到的异常。
//Code //码
private class HttpRequestTask extends AsyncTask<Void, Void, ShiftPlannerModel[]> {
@Override
protected ShiftPlannerModel[] doInBackground(Void... params) {
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<ShiftPlannerModel[]> greeting = restTemplate.getForEntity(URL, ShiftPlannerModel[].class);
Log.i(greeting.getBody().toString(), "doInBackground: ");
return greeting.getBody();
} catch (Exception e) {
Log.e("doInBackgrouExcp", e.getMessage(), e);
}
Below code is for Model object and mapping values: 以下代码用于Model对象和映射值:
@JsonFormat(shape= JsonFormat.Shape.ARRAY)
public class ShiftPlannerModel {
ShiftPlannerModel(){}
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date shiftPlannerDate;
private String resourceName;
private String shiftName;
public ShiftPlannerModel(Date shiftPlannerDate, String resourceName, String shiftName) {
this.shiftPlannerDate = shiftPlannerDate;
this.resourceName = resourceName;
this.shiftName = shiftName;
}
//setters and getters
public static ArrayList<ShiftPlannerModel> fromJson(JSONArray jsonArray) {
JSONObject businessJson;
ArrayList<ShiftPlannerModel> businesses = new ArrayList<ShiftPlannerModel>(jsonArray.length());
// Process each result in json array, decode and convert to business object
for (int i=0; i < jsonArray.length(); i++) {
try {
businessJson = jsonArray.getJSONObject(i);
} catch (Exception e) {
e.printStackTrace();
continue;
}
ShiftPlannerModel business = ShiftPlannerModel.fromJson(businessJson);
if (business != null) {
businesses.add(business);
}
}
return businesses;
}
// Decodes business json into business model object
public static ShiftPlannerModel fromJson(JSONObject jsonObject) {
ShiftPlannerModel shiftPlannerModel = new ShiftPlannerModel();
// Deserialize json into object fields
try {
// shiftPlannerModel.resource = jsonObject.getString("resourceName");
// shiftPlannerModel.ShiftName = jsonObject.getString("resourceId");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date workingDays = sdf.parse(jsonObject.getString("shiftPlannerDate"));
// shiftPlannerModel.shiftPlannerDate = workingDays;
shiftPlannerModel.setResourceName(jsonObject.getString("resourceName"));
shiftPlannerModel.setShiftName(jsonObject.getString("shiftName"));
shiftPlannerModel.setShiftPlannerDate(workingDays);
} catch (JSONException e) {
e.printStackTrace();
return null;
} catch (ParseException e) {
e.printStackTrace();
}
// Return new object
return shiftPlannerModel;
}
}
//json // json
[
[
"2018-04-16",
"Elias",
"I"
],
[
"2018-04-16",
"Sithik",
"II"
],
[
"2018-04-17",
"Vijay",
"II"
],
----------------------------
//Exception while reading the above JSON format //读取上述JSON格式时出现异常
04-18 07:07:09.089 31444-31460/? 04-18 07:07:09.089 31444-31460 /? E/doInBackgrouExcp: Could not read JSON: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]); E / doInBackgrouExcp:无法读取JSON:无法解析日期值“ I”(格式:“ yyyy-MM-dd”):无法解析的日期:“ I”(通过参考链:com.example.admin.myapplication.ShiftPlannerModel [ “ shiftPlannerDate”]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]) org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]); 嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:无法解析日期值“ I”(格式:“ yyyy-MM-dd”):不可解析的日期:“ I”(通过参考链:com.example.admin)。 myapplication.ShiftPlannerModel [“ shiftPlannerDate”])org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法解析日期值“ I”(格式:“ yyyy-MM-dd”):无法解析的日期:“ I” (通过参考链:com.example.admin.myapplication.ShiftPlannerModel [“ shiftPlannerDate”]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to parse Date value 'I' (format: "yyyy-MM-dd"): Unparseable date: "I" (through reference chain: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"]) 嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:无法解析日期值“ I”(格式:“ yyyy-MM-dd”):不可解析的日期:“ I”(通过参考链:com.example.admin)。 myapplication.ShiftPlannerModel [“ shiftPlannerDate”])
ThanksAlot Scary Wombat! 感谢很多可怕的袋熊!
My bad I have checked it everything but didn't notice that webservice which I am consuming is not in Json format. 不好的我已经检查了所有内容,但没有注意到我使用的Web服务不是Json格式。 I will change webservices so that it will return in json format. 我将更改Web服务,使其以json格式返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.