问题描述
我有一个问题反序列化约1GB的JSON文件的问题。当我运行以下代码时,我得到了一个内存异常: using(FileStream sr = new FileStream(myFile.json ,FileMode.Open,FileAccess.Read))
{
using(StreamReader reader = new StreamReader(sr))
{
using(JsonReader jsReader = new JsonTextReader(reader) )
{
JsonSerializer serializer = new JsonSerializer();
dataObject = serializer.Deserialize< T>(jsReader);
}
}
}
异常抛出
Newtonsoft.Json.Linq.JTokenWriter.WriteValue(Int64值)
序列化效果很好,这里是我使用的代码
using(StreamWriter reader = new StreamWriter(myFile.json))
{
using(JsonReader jsWriter = new JsonWriter(reader))
{
JsonTextWriter jsonWriter = new JsonTextWriter(jsWriter){Formatting = Formatting.Indented};
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jsonWriter,dataObject,dataObject.GetType());
jsonWriter.Flush();
}
}}
我在反序列化中做错了吗?你可以帮助建议一种反序列化大json对象的方法吗?
谢谢
根据,您的方法必须工作(因为你通过流读取它应该使你的文件的部分)。我不知道为什么你的代码不起作用。
但是你可以尝试另一种方法,这在下一篇文章中描述 -
I have a problem deserializing a JSON file of about 1GB. When I run the following code I get an out of memory exception:
using (FileStream sr = new FileStream("myFile.json", FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(sr))
{
using (JsonReader jsReader = new JsonTextReader(reader))
{
JsonSerializer serializer = new JsonSerializer();
dataObject = serializer.Deserialize<T>(jsReader);
}
}
}
the exception is thrown by
Newtonsoft.Json.Linq.JTokenWriter.WriteValue(Int64 value)
The serialization works well, here is the code I'm using
using (StreamWriter reader = new StreamWriter("myFile.json"))
{
using (JsonReader jsWriter = new JsonWriter(reader))
{
JsonTextWriter jsonWriter = new JsonTextWriter(jsWriter) { Formatting = Formatting.Indented };
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jsonWriter, dataObject, dataObject.GetType());
jsonWriter.Flush();
}
}}
Am I doing something wrong in the deserialization? Can you help suggesting a way to deserialize big json object?
Thanks
According to Newtonsoft.Json Performance Tips your approach has to work (because you read via stream and it should make portion from your file). I can't figure out why your code doesn't work.
But you can try another approach, that was described in the next article - Parsing Big Records with Json.NET
这篇关于Newtonsoft.Json - 反序列化大对象时出现内存异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!