读取大型文本文件并将响应流作为 JSON 数据

问题描述 投票:0回答:1

我需要读取 30 MB JSON 文件并将结果作为 JSON 数据进行流式传输。我找到了以下代码,但该代码序列化了一个大对象,这会产生内存峰值。如何更改代码以从 JSON 文本文件流式传输部分数据,然后将数据作为 JSON 流式传输回调用应用程序?

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new PushStreamContent((stream, content, context) =>
{
     using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
     using (JsonTextWriter jtw = new JsonTextWriter(sw))
     {
         JsonSerializer ser = new JsonSerializer();
         ser.Serialize(jtw, hugeObject); //don't serialize the entire object. 
         //Read in some of the file and stream it back out until the entire text file is consumed. 
     }
 }, "application/json");

编辑-巨大对象是一个json数据数组:

[
    {
        "agent": "name1",
        "entityCode": "010002",
        "state": "AL",
        "memberType": "U",
        "producerCode": "572714"
    },
    {
        "agent": "name2",
        "entityCode": "010002",
        "state": "AL",
        "memberType": "U",
        "producerCode": "572714"
    }
]

更新 - 我有以下工作,但代码创建了一个大字符串。我想改为流式传输数据。

[HttpGet, Route("/api/GetAllData/{callingApp}")]
public IActionResult GetAllData(
 [RegularExpression(@"^[a-zA-Z0-9_-]*$"), Required, MaxLength(50)] string callingApp)
{
    string fileName = _config.GetSection("AllAgentsPath").Value;
    StreamReader reader = new StreamReader(fileName);

    var buffer = new char[4096];
    int numberRead;
    var stringBuilder = new StringBuilder();
    while ((numberRead = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
    {
        stringBuilder.Append(buffer[..numberRead]);
    }

    return Ok(stringBuilder.ToString());
}
c# asp.net-core asp.net-core-webapi .net-8.0
1个回答
0
投票

感谢 dbc,解决方案贴在下面。非常简单。

[HttpGet, Route("/api/GetAllAgents/{callingApp}")]
public IActionResult GetAllAgents(
 [RegularExpression(@"^[a-zA-Z0-9_-]*$"), Required, MaxLength(50)] string callingApp)
{
    string fileName = _config.GetSection("AllAgentsPath").Value;
    FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    return File(fileStream, "application/json");
}
© www.soinside.com 2019 - 2024. All rights reserved.