【发布时间】:2021-07-05 20:31:35
【问题描述】:
当我运行此代码时,文件被创建,但它是空的,虽然输出看起来不错
import java.io.IOException;
public class JasonTest {
public static void main(String[] args) throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
TestJson tj = new TestJson(13, "kuku");
gson.toJson(tj, new FileWriter("test.json"));
System.out.println(gson.toJson(tj));
}
}
输出:
{
"num": 13,
"text": "kuku"
}
Process finished with exit code 0
【问题讨论】: