JavaScriptSerializer反序列化引发System.OutOfMemoryException

时间:2014-12-19 10:26:12

标签: c# json

我使用下面的代码获取大约17 MB的PPTX文件的数组(如果任何文件小于15 MB,这可以正常工作)。在jss.Deserialize行,它会抛出错误“类型'Exception of System.OutOfMemoryException'”

JavaScriptSerializer jss = new JavaScriptSerializer { MaxJsonLength = 2147483644, RecursionLimit = 100 };

var requestJson = jss.Serialize(new { serverRelativeUrl = serverRelativeUrl, token = token });

WebClient wClient = new WebClient();
wClient.Headers["Content-Type"] = "text/json; charset=utf-8";

var data = wClient.UploadData(serviceMethodURL, "POST", Encoding.UTF8.GetBytes(requestJson));
var responseJson = Encoding.UTF8.GetString(data);

var response = jss.Deserialize<MethodResponse<byte[]>>(responseJson);

有什么想法吗?我坚持这个。请帮忙

1 个答案:

答案 0 :(得分:0)

我使用

解决了这个问题
MethodResponse<byte[]> response = null;
using (System.IO.MemoryStream stream1 = new System.IO.MemoryStream(data))
{
    System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(MethodResponse<byte[]>));
    stream1.Position = 0;
    response = (MethodResponse<byte[]>)ser.ReadObject(stream1);
}
return response;