java - 解析json文件丢失的数据结构 - JAVA

标签 java json

我必须创建一个包含一些对象(名称、字符串列表和日期列表)的 json 文件。问题是当我必须以 LocalDate 格式写入和读取日期时。 如果我写然后打印 jsonObject,日期的格式就可以了。但是当我进行解析并突然打印对象时,日期的格式是错误的。

这是写入文件的代码:

public void aggiungi(){
    JSONObject obj = new JSONObject();
    obj.put("name", this.name);
    JSONArray listMov = new JSONArray();
    JSONArray listData = new JSONArray();
    for(int i =0; i<this.movimenti.size(); i++){
        listMov.add(movimenti.get(i));
        listData.add(data.get(i));
    }
    obj.put("data", listData);
    obj.put("mov", listMov);

    File file=new File("file.json");
    try {
        file.createNewFile();
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(obj.toJSONString());
        fileWriter.flush();
        fileWriter.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(obj);

}

和最终的 println 打印:

{"data":[2015-03-02,2015-04-05,2015-06-10],"mov": 
["F24","PagoBancomat","Bollettino"],"name":"Stefano"}

这是读取代码:

 JSONParser parser = new JSONParser();//per decodifica
        Object obj;
        try {
            obj = parser.parse(new FileReader("file.json"));

            JSONObject jsonObj = (JSONObject) obj;
            String name= (String) jsonObj.get("name");
            JSONArray list = (JSONArray) jsonObj.get("mov");
            JSONArray listData = (JSONArray) jsonObj.get("data");
            System.out.println(jsonObj);
            Iterator<String> iteratorM = list.iterator();
            Iterator<LocalDate> iteratorD = listData.iterator();
            ArrayList<String> mov =new ArrayList<String>(list.size());
            ArrayList<LocalDate> data =new ArrayList<LocalDate>(list.size());
            while(iteratorM.hasNext()){
                mov.add(iteratorM.next());

            }
            while(iteratorD.hasNext()){
                data.add(iteratorD.next());

            }
            ContoCorrente cc = new ContoCorrente(data, mov, name);
            Contatore task = new Contatore(cc);
            this.executeTask(task);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

并打印

{"data":[2015,-3,-2,2015,-4,-5,2015,-6,-10],"mov": 
["F24","PagoBancomat","Bollettino"],"name":"Stefano"}

最后一个打印分隔日期的结构,分隔日、月和年。

我在代码中做错了什么? 谢谢你,抱歉我的英语不好

最佳答案

不要将其设置为 JSON 中的数据对象,而是首先使用日期格式将其解析为字符串,然后将其设置为 JSON 对象中的字符串。

关于java - 解析json文件丢失的数据结构 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50059160/

相关文章:

javascript - 如何创建和克隆 JSON 对象?

java - 检查 REST 方法中是否存在 JSON Key

javascript - 使用 JavaScript 呈现整个页面是一种好习惯吗?

java - Activity 和 Fragment 并发

java - 为什么我们需要在某些语言中使用它之前声明变量,而在其他语言中则不需要?

ios - 如何访问 JSON 数据并将其显示在 XCode Logcat 中,如果响应为空,我想显示警报

java - 如何在 Spring REST Controller 方法中从 RequestBody 获取参数

java - Eclipse 插件开发 : Unable to resolve dependencies

c# - 对象是类,因为枚举是...?

java - Apache Camel Http 和 SSL