流写作Json [英] Streaming to write Json
问题描述
在 ArrayList
中有一大组 JsonObject
。我需要将这些 JsonObject
添加到 JsonArray
中,并将其写入文件中。
private void myWriter (List< JsonObject> jsonObjectHolder,int number)throws IOException
{
System.out.println(开始写JSON文件);
//将所有内容添加到JSONArray中
JsonArray jsonArrayNew = new JsonArray();
for(int i = 0; i< jsonObjectHolder.size(); i ++)
{
//System.out.println(\"inside array:+ i) ;
JsonObject o = jsonObjectHolder.get(i);
System.out.println(inside array+ i +:+ o.get(title));
jsonArrayNew.add(jsonObjectHolder.get(i));
}
System.out.println(Size:+ jsonArrayNew.size());
//写入文件
文件file = new File(items.json);
FileWriter fw = new FileWriter(file);;
fw.write(jsonArrayNew.toString());
fw.flush();
fw.close();
System.out.println(outside array);
}
我不喜欢这种方式。 ArrayList
包含大量数据,我写的方式可能会产生 OutOfMemoryError
。相反,我想Stream并将它们写入文件。
更新
<根据SO用户Alden的回答,这里是如何编辑代码的。
private void myWriter(List< JsonObject> jsonObjectHolder)throws IOException
{
JsonWriter writer = new JsonWriter(new FileWriter(new File(items.json)));
Gson gson = new Gson();
writer.beginArray(); (jsonObject jsonObject:jsonObjectHolder)
{
gson.toJson(jsonObject,writer);
}
writer.endArray();
writer.close();
}
请让我知道这是否是正确的做法。 p>
您可以使用Gson来流式传输您的列表:
<$ p $ (new FileWriter(new File(items.json));}} $ {code> private void myWriter(List< JsonObject> jsonObjectHolder,Gson gson)throws IOException
{
JsonWriter writer = new JsonWriter ));
writer.beginArray(); (jsonObject jsonObject:jsonObjectHolder)
{
gson.toJson(jsonObject,writer);
}
writer.endArray();
writer.close();
$ / code>
假设你有一个 Gson
您可以使用的实例。如果你不这样做,你可以使用 writer.beginObject()
和 writer.endObject()
并手动添加属性到作者,但我不会推荐这个,因为你已经完成了构建 JsonObject
的工作。
I have a large set of JsonObject
s inside a ArrayList
. I need to add these JsonObject
s into a JsonArray
and write it into a file. I am using Gson
and below is my code.
private void myWriter(List<JsonObject> jsonObjectHolder, int number) throws IOException
{
System.out.println("Starting to write the JSON File");
//Add everything into a JSONArray
JsonArray jsonArrayNew = new JsonArray();
for(int i=0;i<jsonObjectHolder.size();i++)
{
//System.out.println("inside array: "+i);
JsonObject o = jsonObjectHolder.get(i);
System.out.println("inside array "+i+": "+o.get("title"));
jsonArrayNew.add(jsonObjectHolder.get(i));
}
System.out.println("Size: "+jsonArrayNew.size());
//Write it to the File
File file= new File("items.json");
FileWriter fw = new FileWriter(file);;
fw.write(jsonArrayNew.toString());
fw.flush();
fw.close();
System.out.println("outside array");
}
I don't like this way. The ArrayList
holds lot of data and the way I write could generate OutOfMemoryError
. Instead, I would like to Stream and write these to the file.
Update
According to the answer of SO user "Alden", here how I edited the code.
private void myWriter(List<JsonObject> jsonObjectHolder) throws IOException
{
JsonWriter writer = new JsonWriter(new FileWriter(new File("items.json")));
Gson gson = new Gson();
writer.beginArray();
for (JsonObject jsonObject : jsonObjectHolder)
{
gson.toJson(jsonObject, writer);
}
writer.endArray();
writer.close();
}
Please let me know whether this is the correct way of doing it.
You can use Gson to stream your list like this:
private void myWriter(List<JsonObject> jsonObjectHolder, Gson gson) throws IOException
{
JsonWriter writer = new JsonWriter(new FileWriter(new File("items.json")));
writer.beginArray();
for (JsonObject jsonObject : jsonObjectHolder)
{
gson.toJson(jsonObject, writer);
}
writer.endArray();
writer.close();
}
This assumes you have a Gson
instance that you can use. If you do not, you can use writer.beginObject()
with writer.endObject()
and manually add properties to the writer, but I wouldn't recommend this because you've already done the work of building a JsonObject
.
这篇关于流写作Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!