How to read json payload of a large response?

╄→尐↘猪︶ㄣ 提交于 2021-01-28 07:44:51

问题


I am developing some code that will read a large json payload returned by the Export API of Mailchimp.

        var response = caller.PostAsync(endpoint, data).Result;

        var exportedData = response.Content.ReadAsStringAsync().Result;

I am currently using the Visual Studio 2017 debugger to try and view the response but I keep getting an error that says:

Cannot obtain value of the local variable or argument because there is not enough memory available.

I ultimately want to extract information from each 'row' of this json data and do operations on them. What do I need to do so that I can view this data without running out of memory?


回答1:


A general approach to operating over large JSON files:

I'd start by avoiding any attempt to decode to string. Instead, grab the content stream of your response, following what is described in my answer here to avoid downloading the entire stream into memory before you get your hands on it.

Now you can:

var stream = await response.Content.ReadAsStreamAsync();

to grab a Stream instead of a string.

Now you can feed the stream directly into a deserializer, according to the information here:

using (StreamReader sr = new StreamReader(stream))
using (JsonReader reader = new JsonTextReader(sr))
{
    JsonSerializer serializer = new JsonSerializer();
    var o = serializer.Deserialize<SomeClassMatchingTheJsonSchema>(reader);
}

If you're still choking on memory, you might need to consider operating at a lower-level using JsonTextReader




回答2:


try

var exportedData = await response.Content.ReadAsAsync<model>();

also make sure your method is async

public async Task<returntype> MethodName()

let "model", in ReadAsAsync, be the name of the object model you're going to map the data to and let "returntype", next to Task in the method name, be whatever datatype your method is returning

can you update your question and show the entire method. I would like to take a closer look at it



来源:https://stackoverflow.com/questions/56398881/how-to-read-json-payload-of-a-large-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!