【问题标题】:parse a json file lost structure of data - JAVA解析一个json文件丢失的数据结构 - JAVA
【发布时间】:2018-04-27 09:18:29
【问题描述】:

我必须创建一个包含一些对象(名称、字符串列表和日期列表)的 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"}

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

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

【问题讨论】:

    标签: java json


    【解决方案1】:

    不是在 JSON 中设置为数据对象,而是先使用 dateformatters 将其解析为字符串,然后在 JSON 对象中将其设置为字符串。

    【讨论】:

    • 我是第一次使用json,请问你能告诉我怎么做吗?
    【解决方案2】:

    使用 DateTimeFormatter 将 LocalDate 转换为 String 值,假设 dataArrayList&lt;LocalDate&gt; 的对象;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      相关资源
      最近更新 更多