英文:
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.
{
"@adobe/css-tools@4.2.0": {
"licenses": "MIT",
"repository": "https://github.com/adobe/css-tools",
"publisher": "TJ Holowaychuk",
"email": "tj@vision-media.ca",
"path": "\\node_modules\\@adobe\\css-tools",
"licenseFile": "\\node_modules\\@adobe\\css-tools\\LICENSE"
},
"@ampproject/remapping@2.2.0": {
"licenses": "Apache-2.0",
"repository": "https://github.com/ampproject/remapping",
"publisher": "Justin Ridgewell",
"email": "jridgewell@google.com",
"path": "\\node_modules\\@ampproject\\remapping",
"licenseFile": "\\node_modules\\@ampproject\\remapping\\LICENSE"
},
"@angular-devkit/architect@0.1402.11": {
"licenses": "MIT",
"repository": "https://github.com/angular/angular-cli",
"publisher": "Angular Authors",
"path": "\\node_modules\\@angular-devkit\\architect",
"licenseFile": "\\node_modules\\@angular-devkit\\architect\\LICENSE"
}
}
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<T> ReadAsync<T>(string filePath)
{
try
{
using FileStream stream = File.OpenRead(filePath);
return await JsonSerializer.DeserializeAsync<T>(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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论