简体   繁体   English

HttpMessageNotReadableException:无法读取JSON:无法解析日期值“ I”

[英]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.

相关问题 HttpMessageNotReadableException:无法读取JSON:读取超时 - HttpMessageNotReadableException: Could not read JSON: Read timed out HttpMessageNotReadableException:无法读取 JSON:使用 Spring 引导和 Android 无法识别的字段 - HttpMessageNotReadableException: Could not read JSON: Unrecognized field using Spring boot and Android 这个错误是什么&#39;org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法反序列化实例&#39;是什么意思? - What does this error 'org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance' mean? 对Spring MVC控制器的POST结果为“ HttpMessageNotReadableException:无法读取JSON:未找到合适的构造函数” - POST to Spring MVC controller results in “HttpMessageNotReadableException: Could not read JSON: No suitable constructor found” HttpMessageNotReadableException:JSON 解析错误:无法构造实例 - HttpMessageNotReadableException: JSON parse error: Cannot construct instance HttpMessageNotReadableException:JSON 解析错误:无法从字符串反序列化“int”类型的值 - HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `int` from String 无法从JSON解析“日期” - Failed to parse “Date” from JSON HttpMessageNotReadableException:JSON 解析错误:无法识别的令牌“嬀崀” - HttpMessageNotReadableException: JSON parse error: Unrecognized token '嬀崀' SpringBoot - HttpMessageNotReadableException:JSON 解析错误:java.net.SocketTimeoutException - SpringBoot - HttpMessageNotReadableException: JSON parse error: java.net.SocketTimeoutException 无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM