将json对象反序列化为C#对象
我正在从文件中获取json。它可能有不同的结构,例如,它可能看起来像这样:将json对象反序列化为C#对象,c#,json,C#,Json,我正在从文件中获取json。它可能有不同的结构,例如,它可能看起来像这样: { "root": { "name": "LWindow", "children": [{ "name": "Label", "children": []
{
"root": {
"name": "LWindow",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
},
{
"name": "Label",
"children": []
},
{
"name": "Radio",
"children": []
},
{
"name": "Checkbox",
"children": []
},
{
"name": "Label",
"children": []
},
{
"name": "Label",
"children": []
}
]
}
}
{
"root": {
"name": "LWindow",
"children": [{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
}
]
},
{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
}
]
},
{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Radio",
"children": []
}
]
},
{
"name": "Label",
"children": []
},
{
"name": "Button",
"children": []
}
]
}
}
string json = File.ReadAllText("File path");
Root root = JsonSerializer.Deserialize<Root>(json);
private void Traverse(RootElement element)
{
foreach (var child in element.Children)
{
// Do something with the child
this.Traverse(child); // Traverse the child recursively
}
}
或者像这样:
{
"root": {
"name": "LWindow",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
},
{
"name": "Label",
"children": []
},
{
"name": "Radio",
"children": []
},
{
"name": "Checkbox",
"children": []
},
{
"name": "Label",
"children": []
},
{
"name": "Label",
"children": []
}
]
}
}
{
"root": {
"name": "LWindow",
"children": [{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
}
]
},
{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
}
]
},
{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Radio",
"children": []
}
]
},
{
"name": "Label",
"children": []
},
{
"name": "Button",
"children": []
}
]
}
}
string json = File.ReadAllText("File path");
Root root = JsonSerializer.Deserialize<Root>(json);
private void Traverse(RootElement element)
{
foreach (var child in element.Children)
{
// Do something with the child
this.Traverse(child); // Traverse the child recursively
}
}
我需要的是将其反序列化为c#对象。我已经有了一个类,它描述了json:
public partial class Root
{
public RootElement RootRoot { get; set; }
}
public partial class RootElement
{
public string Name { get; set; }
public List<RootElement> Children { get; set; }
}
公共部分类根目录
{
公共根元素RootRoot{get;set;}
}
公共部分类根元素
{
公共字符串名称{get;set;}
公共列表子项{get;set;}
}
但我并不真正理解如何从我的根元素中提取嵌套对象,因为嵌套对象可以有不同的结构,也可以有自己的嵌套对象
我忘了去梅蒂安。我已经将json反序列化到我的根目录中:
public static T DeserializeJson<T>(String pathToJSON)
{
using (StreamReader file = File.OpenText(pathToJSON))
{
JsonSerializer serializer = new JsonSerializer();
return (T)serializer.Deserialize(file, typeof(T));
}
}
publicstatict反序列化JSON(字符串pathToJSON)
{
使用(StreamReader file=file.OpenText(pathToJSON))
{
JsonSerializer serializer=新的JsonSerializer();
返回(T)序列化程序。反序列化(文件,typeof(T));
}
}
我觉得您的模型与您可能得到的JSON结构相对应。
在这种情况下,您只需要读取文件并执行反序列化。应该是这样的:
{
"root": {
"name": "LWindow",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
},
{
"name": "Label",
"children": []
},
{
"name": "Radio",
"children": []
},
{
"name": "Checkbox",
"children": []
},
{
"name": "Label",
"children": []
},
{
"name": "Label",
"children": []
}
]
}
}
{
"root": {
"name": "LWindow",
"children": [{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
}
]
},
{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Edit",
"children": []
}
]
},
{
"name": "FormItem",
"children": [{
"name": "Label",
"children": []
},
{
"name": "Radio",
"children": []
}
]
},
{
"name": "Label",
"children": []
},
{
"name": "Button",
"children": []
}
]
}
}
string json = File.ReadAllText("File path");
Root root = JsonSerializer.Deserialize<Root>(json);
private void Traverse(RootElement element)
{
foreach (var child in element.Children)
{
// Do something with the child
this.Traverse(child); // Traverse the child recursively
}
}
在.net 5中,您可以使用.net中内置的
System.Text.Json
using System.Text.Json;
类中属性的名称应与JSON字段中的属性名称相同
或者用JSON名称对其进行注释,如下所示
public partial class Root
{
[JsonPropertyName("root")]
public RootElement RootRoot { get; set; }
}
public partial class RootElement
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("children")]
public List<RootElement> Children { get; set; }
}
公共部分类根目录
{
[JsonPropertyName(“根”)]
公共根元素RootRoot{get;set;}
}
公共部分类根元素
{
[JsonPropertyName(“名称”)]
公共字符串名称{get;set;}
[JsonPropertyName(“儿童”)]
公共列表子项{get;set;}
}
更新
根据您的评论,您需要循环查看您的孩子,或者创建一个方法来搜索孩子列表
完整工作代码
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Text.Json;
使用System.Text.Json.Serialization;
名称空间控制台AP3
{
公共类根
{
[JsonPropertyName(“根”)]
公共根元素RootRoot{get;set;}
}
公共类根元素
{
[JsonPropertyName(“名称”)]
公共字符串名称{get;set;}
[JsonPropertyName(“儿童”)]
公共列表子项{get;set;}
}
班级计划
{
静态IEnumerable GetElements(列表chelderins、字符串serchName)
{
foreach(chelderins中的变量child)
{
如果(child.Name==serchName)返回子项;
}
}
静态void Main(字符串[]参数)
{
字符串jsonContent=File.ReadAllText(“File.json”);
Root op=JsonSerializer.Deserialize(jsonContent);
//现在op包含来自json的值
var Labels=GetElements(op.RootRoot.Children,“Label”).ToList();
//标签现在是一个包含所有childen contains name=label的列表
var Checkbox=GetElements(op.RootRoot.Children,“Checkbox”).ToList();
}
}
}
您尝试了什么?您搜索过“将json反序列化为C#”吗?我打赌有成百上千的问题。其中至少有一个可以肯定地告诉你你在寻找什么。要反序列化为一个动态对象,请参阅转换为一个特定的类,请参阅这是否回答了你的问题?谢谢我已经将json文件反序列化到根类。问题是从这个类中排除嵌套对象。是的,我忘了提到。我已经得到了我的rootobject类。使用此方法实现了public static T DeserializeJson(String pathToJSON){使用(StreamReader file=file.OpenText(pathToJSON)){JsonSerializer serializer=new JsonSerializer();return(T)serializer.Deserialize(file,typeof(T));}在这种情况下,似乎您需要一个递归方法来遍历子级,并且对于每个子级,调用相同的递归方法。添加了一个如何递归遍历元素树的示例。是的,将json反序列化到根级没有问题。我在提取嵌套对象时遇到问题。像标签或表格。现在所有这些都是RootElement类的实例,你需要在你的孩子身上循环,我更新了答案,请检查一下,让我知道它是否解决了你的问题