[英]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 JsonSerializer
像
System.Text.Json
的某种方式,通过在使用JsonDocument
或JsonSerializer
时加载到某个对象中
Also is there some way I can minify this JSON after loading it from file从文件加载后,我还有什么方法可以缩小这个 JSON
I have seen some solution for newtonjson我已经看到了 newtonjson 的一些解决方案
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.