【问题标题】:HttpMessageNotReadableException: Could not read JSON: Failed to parse Date value 'I'HttpMessageNotReadableException:无法读取 JSON:无法解析日期值“I”
【发布时间】:2018-04-18 04:34:02
【问题描述】:

以下是代码,json文件和我在执行代码时遇到的异常。

//代码

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);
                }

以下代码用于模型对象和映射值:

    @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

  [
        [
            "2018-04-16",
            "Elias",
            "I"
        ],
        [
            "2018-04-16",
            "Sithik",
            "II"
        ],
        [
            "2018-04-17",
            "Vijay",
            "II"
        ],
----------------------------

//读取上述JSON格式时出现异常

04-18 07:07:09.089 31444-31460/? E/doInBackgrouExcp:无法读取 JSON:无法解析日期值“I”(格式:“yyyy-MM-dd”): 无法解析的日期:“I”(通过引用链: 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"]); 嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:解析失败 日期值'I'(格式:“yyyy-MM-dd”):无法解析的日期:“I”(通过 参考链: com.example.admin.myapplication.ShiftPlannerModel["shiftPlannerDate"])

【问题讨论】:

  • 所以看起来您的代码将第三个字段误认为是第二个字段,为什么不显示有问题的代码?
  • 嗨,Scary 用模型对象编辑了我的问题。
  • 你确定这是有效的 JSON 吗?

标签: java android spring rest spring-boot


【解决方案1】:

非常感谢可怕的袋熊!

糟糕的是,我已经检查了所有内容,但没有注意到我正在使用的 web 服务不是 Json 格式。我将更改 web 服务,以便它以 json 格式返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 2021-01-23
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多