如何读取较大响应的json有效负载? [英] How to read json payload of a large response?

查看:82
本文介绍了如何读取较大响应的json有效负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些代码,该代码将读取Mailchimp的Export API返回的大型json有效负载.

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;

我当前正在使用Visual Studio 2017调试器尝试查看响应,但我不断收到错误消息:

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

由于没有足够的可用内存而无法获取局部变量或参数的值.

我最终想从此json数据的每个行"中提取信息并对其进行操作.我需要怎么做才能在不耗尽内存的情况下查看此数据?

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?

推荐答案

对大型JSON文件进行操作的一般方法:

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.

现在您可以:

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

抓取 Stream 而不是 string .

根据信息如果您仍然对内存感到窒息,则可能需要考虑使用 JsonTextReader

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

这篇关于如何读取较大响应的json有效负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆