使用.NET Core读取具有唯一父元素的JSON文件。

huangapple go评论68阅读模式
英文:

Read json file with unique parent elements in .net core

问题

以下是翻译好的内容:

如何在.NET中读取JSON文件。

问题是父元素是唯一的,所以很难创建一个表示这个结构的类。
当我使用下面的代码时,它返回一个空数组。当我尝试在在线工具中将其转换为C#模型时,它为每个元素创建一个新类。

理想的输出应该是一个包含所有信息的平面数组。

这是我有的类

public class NmpLicense
{
    public string licenses { get; set; }
    public string repository { get; set; }
    public string publisher { get; set; }
    public string email { get; set; }
    public string path { get; set; }
    public string licenseFile { get; set; }
}

这是我读取文件的方式

public static async Task<T> ReadAsync<T>(string filePath)
{
    try
    {
        using FileStream stream = File.OpenRead(filePath);
        return await JsonSerializer.DeserializeAsync<T>(stream);
    }
    catch (JsonException ex)
    {

    }

    return default(T);

}
英文:

How do I read a json file in .net.

The issue is parent element is unique so it is hard to create a class to represent this structure.
When I use the code below it returns an empty array. When I try to convert this in an online tool to C# model, it creates a new class for each element.

The ideal output should be a flat array with all information.

{
  &quot;@adobe/css-tools@4.2.0&quot;: {
    &quot;licenses&quot;: &quot;MIT&quot;,
    &quot;repository&quot;: &quot;https://github.com/adobe/css-tools&quot;,
    &quot;publisher&quot;: &quot;TJ Holowaychuk&quot;,
    &quot;email&quot;: &quot;tj@vision-media.ca&quot;,
    &quot;path&quot;: &quot;\\node_modules\\@adobe\\css-tools&quot;,
    &quot;licenseFile&quot;: &quot;\\node_modules\\@adobe\\css-tools\\LICENSE&quot;
  },
  &quot;@ampproject/remapping@2.2.0&quot;: {
    &quot;licenses&quot;: &quot;Apache-2.0&quot;,
    &quot;repository&quot;: &quot;https://github.com/ampproject/remapping&quot;,
    &quot;publisher&quot;: &quot;Justin Ridgewell&quot;,
    &quot;email&quot;: &quot;jridgewell@google.com&quot;,
    &quot;path&quot;: &quot;\\node_modules\\@ampproject\\remapping&quot;,
    &quot;licenseFile&quot;: &quot;\\node_modules\\@ampproject\\remapping\\LICENSE&quot;
  },
  &quot;@angular-devkit/architect@0.1402.11&quot;: {
    &quot;licenses&quot;: &quot;MIT&quot;,
    &quot;repository&quot;: &quot;https://github.com/angular/angular-cli&quot;,
    &quot;publisher&quot;: &quot;Angular Authors&quot;,
    &quot;path&quot;: &quot;\\node_modules\\@angular-devkit\\architect&quot;,
    &quot;licenseFile&quot;: &quot;\\node_modules\\@angular-devkit\\architect\\LICENSE&quot;
  }
}

This is the class I have

public class NmpLicense
{
    public string licenses { get; set; }
    public string repository { get; set; }
    public string publisher { get; set; }
    public string email { get; set; }
    public string path { get; set; }
    public string licenseFile { get; set; }
}

And this is how I read the file

 public static async Task&lt;T&gt; ReadAsync&lt;T&gt;(string filePath)
    {
        try
        {
            using FileStream stream = File.OpenRead(filePath);
            return await JsonSerializer.DeserializeAsync&lt;T&gt;(stream);
        }
        catch (JsonException ex)
        {

        }

        return default(T);

    }

答案1

得分: -1

以下是代码的翻译部分:

// 理想的输出应该是一个包含所有信息的平面数组。

尝试:

public static async Task<T> ReadAsync<T>(string fullPath)
{
    try
    {
        var jsonData = System.IO.File.ReadAllText(fullPath); 
        var parsedJson = JObject.Parse(jsonData); 
        var arrays = parsedJson.Root.Values()
        .Select(x => new NmpLicense
        {
            licenses = (string)x["licenses"],
            repository = (string)x["repository"],
            publisher = (string)x["publisher"],
            email = (string)x["email"],
            path = (string)x["path"],
            licenseFile = (string)x["licenseFile"],
        }).ToArray();
    }
    catch (JsonException ex)
    {

    }

    return default(T);

}
result:
[![enter image description here][1]][1]

请注意,这是代码的翻译部分,我已经去掉了问题的内容,只返回了您要的翻译。

英文:

> The ideal output should be a flat array with all information.

Try:

public static async Task&lt;T&gt; ReadAsync&lt;T&gt;(string fullPath)
    {
        try
        {
            var jsonData = System.IO.File.ReadAllText(fullPath); 
            var parsedJson = JObject.Parse(jsonData); 
            var arrays = parsedJson.Root.Values()
            .Select(x =&gt; new NmpLicense
            {
                licenses = (string)x[&quot;licenses&quot;],
                repository = (string)x[&quot;repository&quot;],
                publisher = (string)x[&quot;publisher&quot;],
                email = (string)x[&quot;email&quot;],
                path = (string)x[&quot;path&quot;],
                licenseFile = (string)x[&quot;licenseFile&quot;],
            }).ToArray();
        }
        catch (JsonException ex)
        {

        }

        return default(T);

    }

result:
使用.NET Core读取具有唯一父元素的JSON文件。

huangapple
  • 本文由 发表于 2023年6月16日 09:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定