简体   繁体   English

在 .net core 3.1 中使用 System.Text.Json 从文件加载后从 json 中删除空格

[英]Remove white spaces from json after loading it from file using System.Text.Json in .net core 3.1

I am using .NET Core 3.1 with System.Text.Json I am reading JSON from file我正在使用 .NET Core 3.1 和System.Text.Json我正在从文件中读取 JSON

var jsonFilename = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "WellKnownConfig.json");
if (System.IO.File.Exists(jsonFilename))
{
    var fileContent = System.IO.File.ReadAllText(jsonFilename);
    if (!string.IsNullOrWhiteSpace(fileContent))
    {
         //var o = JsonDocument.Parse(fileContent);                        
         Result = new OkObjectResult(fileContent);
    }
    else
    {
        Result = new NoContentResult();
    }
}

The problem is it is having whitespaces.问题是它有空格。 anyway, I can remove white spaces without string parsing.无论如何,我可以在不进行字符串解析的情况下删除空格。 Like some way from System.Text.Json by loading into some object while using JsonDocument or JsonSerializerSystem.Text.Json的某种方式,通过在使用JsonDocumentJsonSerializer时加载到某个对象中

Also is there some way I can minify this JSON after loading it from file从文件加载后,我还有什么方法可以缩小这个 JSON

I have seen some solution for newtonjson我已经看到了 newtonjson 的一些解决方案

Minify a json string using .NET 使用 .NET 缩小 json 字符串

Following Did the trick.跟着做的伎俩。 I hope it will help other's as well我希望它也能帮助其他人

 var fileContent = System.IO.File.ReadAllText(jsonFilename);
                    if (!string.IsNullOrWhiteSpace(fileContent) && Utility.IsValidJson(fileContent))
                    {
                        var obj = JsonSerializer.Deserialize<object>(fileContent);
                        Result = new OkObjectResult(obj);
                    }
                    else
                    {
                        Result = new NoContentResult();
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Text.Json access Json object of json object using c# .net core 3.1 - System.Text.Json access Json object of json object using c# .net core 3.1 使用 System.Text.Json 从 JSON 中动态删除属性 - dynamically remove property from JSON using System.Text.Json .NET CORE 3.1 ApiController 装饰器是否使用 System.Text.Json 返回 json? - .NET CORE 3.1 Does ApiController decorator return json using System.Text.Json? 使用 System.Text.Json 从 json 文件读取到 IEnumerable - Read from json file into IEnumerable using System.Text.Json 如何在.net core 3.1中使用System.Text.Json获取对象内部对象的值 - How to get value of object inside object using System.Text.Json in .net core 3.1 使用 .NET core 3.0/System.text.Json 解析 JSON 文件 - Parsing a JSON file with .NET core 3.0/System.text.Json 如何从 System.Text.Json 中的 JSON 中删除中间属性? - How to remove a middle property from JSON in System.Text.Json? 使用 .NET Core 3.1 中的 System.Text.Json 反序列化会导致 null 属性值 - Deserialization with System.Text.Json in .NET Core 3.1 results in null property values C#.Net Core 3.1 System.Text.Json 序列化时忽略空集合 - C# .Net Core 3.1 System.Text.Json Ignore empty collection in serialization 使用 dotnet Core 3 System.Text.Json Api 流式传输 JSON - Streaming JSON using the dotnet Core 3 System.Text.Json Api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM