java JsonWriter IOException不完整文档
我用的是com。谷歌。格森。流动JsonWriter与StringWriter一起编写有点长的JSON
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
然后迭代集合并编写JSON
jsonWriter.name("Elements").beginArray();
Collection<Element> collection = someObject.getCollection();
for(Element e : collection){
jsonWriter.beginObject()
.name("Id").value(e.getId())
.endObject();
}
jsonWriter.endArray();
我的问题是,当我试图关闭writer时,我得到了一个IOException:不完整的文档很强,并且看起来流不能刷新所有的数据或者什么,因为JSON在元素的中间结束。
像这样: 该集合实际上是一个哈希映射。值()。我有一种预感,那是因为HashMap没有同步或类似的东西 编辑:我正在关闭编写器并通过以下方式获取字符串:{
"Id": "1245
jsonWriter.flush();
jsonWriter.close();
String json = stringWriter.toString();
共 (0) 个答案