【问题标题】:compress list of json obects to gzip file in JAVA将 json 对象列表压缩为 JAVA 中的 gzip 文件
【发布时间】:2021-03-18 16:46:42
【问题描述】:

我想使用 JAVA 将 JSON 对象的列表转换为 gzip 文件

下面是我的 JSON 文件

[
    {
        "id": "01",
        "Status": "Open",
        "siteId": "01",
        "siteName": "M1"
    },
    {
        "id": "02",
        "Status": "Open",
        "siteId": "02",
        "siteName": "M2"
    },
    {
        "id": "03",
        "Status": "Open",
        "siteId": "03",
        "siteName": "M3"
    }
]

写到现在的代码:

public static void main(String args[]) throws IOException
{
    final ObjectMapper objectMapper = new ObjectMapper();
    String fileName = "jsonList.json";
    ClassLoader classLoader = className.class.getClassLoader();
    File file = new File(classLoader.getResource(fileName).getFile());
    System.out.println("File Found : " + file.exists());
    List<document> list = objectMapper.readValue(file,new TypeReference<List<document>>(){});
    System.out.println("List of json objects");
    //code to compress list of json objects (list)
}

文档类

public class document {
    public String id;
    public String status;
   public String sideId;
   public String siteName;

}

请建议我压缩列表的代码 谢谢!

【问题讨论】:

    标签: java json gzip


    【解决方案1】:

    你可以使用这样的东西,StudentL:

        OutputStream outputStream = new FileOutputStream("output_file.zip");
        GZIPOutputStream gzipOuputStream = new GZIPOutputStream(outputStream);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOuputStream);
    
        for (Document doc : list) {
            objectOutputStream.writeObject(doc);
        }
    
        objectOutputStream.close();
    

    我想这是为了练习,否则认为你所做的事情没有多大意义。您不需要阅读 JSON 并执行整个解组操作;您可以简单地获取现有文件并 gzip 压缩它。

    奖励编辑:您的代码中有两件事往往会激怒 Java 开发人员。首先是在Java中类的名字通常写成Camel case。第二个是不要在花括号前使用换行符,就像在 C/C++ 和其他语言中那样。

    错误:

    public class Whatever 
    {
    

    对:

    public class Whatever {
    

    再见!

    【讨论】:

    • 当然,如果我可以帮助进行更多练习,请直接与我联系,当我有空闲时间时,我会很高兴修复这类事情?
    • 当然,谢谢
    • 你能帮我解决这个问题吗stackoverflow.com/questions/66708636/…
    【解决方案2】:

    我认为你可以这样做:

    List<Map<String, String>> jsonList = ...;
     
    // Create a FileOutputStream
    FileOutputStream fileOutputStream 
        = new FileOutputStream("/path/to/file");
     
    // Wrap the FileOutputStream in a BufferedOutputStream
    // so that we write in larger chunks for performance
    BufferedOutputStream bufferedOutputStream 
        = new BufferedOutputStream(fileOutputStream);
     
    // Wrap the BufferedOutputStream in a GIZPOutputStream
    // so that when we write to the gzipOutputStream,
    // we first compress then buffer then write to the file.
    GZIPOutputStream compressedOutputStream 
        = new GZIPOutputStream(bufferedOutputStream);
     
    // Write the JSON map into the compressedOutputStream
    objectMapper.writeValue(compressedOutputStream, jsonList);
    

    这里是Jackson Library你可以参考;这是使用对象映射器和输出流将对象压缩到文件的详细explanation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多