【问题标题】:Empty json file when serialising with gson使用 gson 序列化时为空 json 文件
【发布时间】:2021-10-04 10:49:42
【问题描述】:

我想将一个 Student 对象序列化为一个 student.json 文件, 在我的代码中是学生。

主要思想是我创建一个 json 字符串,从中创建一个学生,然后再次从学生对象创建一个 json 字符串,最后将学生对象序列化为一个 json 文件。

这是我的代码:

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.Writer; 
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.file.Files;
public class JaJson {

    public static void main(String args[]) {  
        System.out.println("Hello");  
        String jsonString = "{\"name\":\"Mahesh\", \"age\":21}"; 

        GsonBuilder builder = new GsonBuilder(); 
        builder.setPrettyPrinting(); 

        Gson gson = builder.create(); 
        Student student = gson.fromJson(jsonString, Student.class);
        System.out.println("student object");    
        System.out.println(student);    

        jsonString = gson.toJson(student);
        System.out.println("student string");  
        System.out.println(jsonString); 
        String sFileName = "student.json";
        try{
    //Writer writer = Files.newBufferedWriter(Paths.get(sFileName));
    //gson.toJson(student, writer);
            Writer writer = new FileWriter(sFileName);
            gson.toJson(student, writer);
        } catch(IOException ex){
            System.out.println (ex.toString());
            System.out.println("Could not find file " + sFileName);
        }

    }  
    public JaJson(){
        System.out.println("what time is it ?");
    }

}

这里是 Student.java

public class Student { 
   private String name; 
   private int age; 
   public Student(){} 
   
   public String getName() { 
      return name; 
   }
   
   public void setName(String name) { 
      this.name = name; 
   } 
   
   public int getAge() { 
      return age; 
   }
   
   public void setAge(int age) { 
      this.age = age; 
   }
   
   public String toString() { 
      return "Student [ name: "+name+", age: "+ age+ " ]"; 
   }  
}

这是程序的输出:

Hello
student object
Student [ name: Mahesh, age: 21 ]
student string
{
  "name": "Mahesh",
  "age": 21
}

但它会产生一个我不明白的空 student.json ...

知道缺少什么吗?

问候

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    我忘了补充:

        writer.flush(); //flush data to file   <---
        writer.close(); //close write  
    

    在我的代码中:

    try{
        //Writer writer = Files.newBufferedWriter(Paths.get(sFileName));
        //gson.toJson(student, writer);
                Writer writer = new FileWriter(sFileName);
                gson.toJson(student, writer);
                writer.flush(); //flush data to file   <---
                writer.close(); //close write  
            } catch(IOException ex){
                System.out.println (ex.toString());
                System.out.println("Could not find file " + sFileName);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2012-02-02
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多