gpt4 book ai didi

java - 使用 GSON JsonWriter 将 pretty-print json 输出写入文件输出流

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:11 27 4
gpt4 key购买 nike

<分区>

我很难使用 Gson 库在文件上编写 pretty-print json 字符串。我使用的代码在这里:

    Gson gs = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();     

JsonWriter jsonWriter = null;
try {
jsonWriter = new JsonWriter(new OutputStreamWriter(stream, "UTF-8"));
jsonWriter.beginArray();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

for (Feed feed : feeds) {
gs.toJson(feed, Feed.class, jsonWriter);
}

try {
jsonWriter.endArray();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
jsonWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}

这里的流只是一个文件输出流。即使我已经启用了 pretty-print ,但我仍然在文件中得到未格式化的 json 字符串。

感谢任何帮助。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com