Java中将json对象保存到本地文件中

Java中将json对象保存到本地文件中

 

实践笔记之一++++

废话不多,直接上代码:

 // Args in JSON format
        String myArgs = String.format("{\"end_date\": \"%s\", \"page_size\": \"%s\", \"start_date\": \"%s\", \"advertiser_id\": \"%s\", \"group_by\": %s, " +
                "\"page\": \"%s\"}", end_date,  page_size, start_date, advertiser_id, group_by, page);


        JSONObject object = JSONObject.parseObject(get(myArgs));
        String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);


        String path="C:\\Users\\ThinkPad\\Desktop";
        //判断文件是否存在
        File file = new File(path+"/广告计划.txt");
        if (file.exists()) {
            System.out.println("文件存在");
        } else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件创建成功");
        }

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(pretty.getBytes());
            fileOutputStream.close();
            System.out.println("json数据保存到成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
        }

代码不足之处欢迎交流!!!

上一篇:SpringBoot整合Druid数据源


下一篇:Druid中使用log4j2进行日志输出