You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中JSON文件读写LocalDate格式异常问题排查

解决LocalDate在JSON读写中的格式异常问题

看起来你遇到的是org.json库不原生支持Java 8 LocalDate类型导致的问题——写入时因为LocalDate的toString()刚好是ISO格式,所以打印JSONObject时看起来正常,但解析时JSON里的日期本质是字符串,直接还原成LocalDate时没有正确转换,就会出现格式错误或异常。

问题根源

org.json是轻量级JSON库,没有内置Java 8日期时间类型的序列化/反序列化支持。当你直接把LocalDate对象put到JSONObject/JSONArray中时,它会自动调用LocalDate.toString()(生成yyyy-MM-dd格式的字符串)存入JSON;但解析时,JSON里的内容是字符串类型,如果你直接尝试把它当作LocalDate对象取出(比如用getObject()),就会因为类型不匹配出错,或者手动强转也会失败。

修正方案:手动处理字符串转换

我们需要在写入时主动把LocalDate转成标准ISO字符串,解析时再把字符串转回LocalDate。

1. 写入JSON的修正代码

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONObject;

public void aggiungi() {
    JSONObject obj = new JSONObject();
    obj.put("name", this.name);

    // 处理字符串列表(假设你有String列表stringList)
    JSONArray listStr = new JSONArray();
    for (String str : stringList) {
        listStr.put(str);
    }
    obj.put("stringList", listStr);

    // 处理LocalDate列表(核心修正:主动转成ISO字符串)
    JSONArray listDate = new JSONArray();
    DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
    for (LocalDate date : dateList) {
        listDate.put(date.format(dateFormatter));
    }
    obj.put("dateList", listDate);

    // 写入文件的逻辑(比如用FileWriter)
    // ...
}

2. 解析JSON的修正代码

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

public YourClass leggi(JSONObject obj) {
    String name = obj.getString("name");

    // 解析字符串列表
    JSONArray strArray = obj.getJSONArray("stringList");
    List<String> stringList = new ArrayList<>();
    for (int i = 0; i < strArray.length(); i++) {
        stringList.add(strArray.getString(i));
    }

    // 解析LocalDate列表(核心修正:字符串转LocalDate)
    JSONArray dateArray = obj.getJSONArray("dateList");
    List<LocalDate> dateList = new ArrayList<>();
    DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
    for (int i = 0; i < dateArray.length(); i++) {
        String dateStr = dateArray.getString(i);
        LocalDate date = LocalDate.parse(dateStr, dateFormatter);
        dateList.add(date);
    }

    // 构建你的对象并返回
    YourClass instance = new YourClass();
    instance.setName(name);
    instance.setStringList(stringList);
    instance.setDateList(dateList);
    return instance;
}

额外提示

如果你的项目可以切换到更强大的JSON库(比如Jackson),可以通过添加jackson-datatype-jsr310模块来自动支持LocalDate的序列化/反序列化,无需手动转换:

  • 依赖(Maven):
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
  • 配置ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
// 序列化时输出ISO格式日期
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

这样就能直接读写LocalDate对象,不用手动处理字符串转换了。

内容的提问来源于stack exchange,提问作者Alice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:02:04