解析JSON文件丢失数据结构-Java



我必须创建一个包含某些对象的JSON文件(名称,字符串列表和日期列表(。问题是我必须以local日期格式编写和阅读日期。如果我写信,然后打印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中的数据对象设置为数据对象,首先使用dateformatters将其解析为字符串,然后将其设置为JSON对象,为字符串。

使用dateTimeFormatter将localdate转换为字符串值,假设dataArrayList<LocalDate>;

的对象
 JSONArray listMov = new JSONArray();
 JSONArray listData = new JSONArray();
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 for(int i =0; i<this.movimenti.size(); i++){
    listMov.add(movimenti.get(i));
    listData.add(data.get(i).format(formatter));
}

最新更新