我正在从文件中获取 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": []
}
]
}
}
我需要的是将其反序列化为 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; }
}
但我真的不明白如何从我的 RootElement 中提取嵌套对象,因为嵌套对象可能有不同的结构,也可能有自己的嵌套对象。
我忘记提到了。我已经将 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));
}
}
答案 0 :(得分:0)
我似乎您的模型与您可能获得的 JSON 结构相对应。 在这种情况下,您只需要读取文件并执行反序列化。应该是这样的:
string json = File.ReadAllText("File path");
Root root = JsonSerializer.Deserialize<Root>(json);
一旦你有了 Root 对象,你就可以检查它是否有任何级别的孩子。
更新: 要遍历子项,您应该添加这样的递归方法:
private void Traverse(RootElement element)
{
foreach (var child in element.Children)
{
// Do something with the child
this.Traverse(child); // Traverse the child recursively
}
}
答案 1 :(得分:0)
使用 .net 5,您可以使用 System.Text.Json
内置于 .net
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; }
}
根据您的评论,您需要遍历您的孩子或创建一种搜索孩子列表的方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ConsoleApp3
{
public class Root
{
[JsonPropertyName("root")]
public RootElement RootRoot { get; set; }
}
public class RootElement
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("children")]
public List<RootElement> Children { get; set; }
}
class Program
{
static IEnumerable<RootElement> GetElements( List<RootElement> chelderins, string serchName)
{
foreach (var child in chelderins)
{
if (child.Name == serchName)yield return child;
}
}
static void Main(string[] args)
{
string jsonContent = File.ReadAllText("file.json");
Root op = JsonSerializer.Deserialize<Root>(jsonContent);
// now op is contains value from your json
var Labels= GetElements(op.RootRoot.Children,"Label").ToList() ;
// labels now is an list with all childen contains name = lablel
var checkBoxes = GetElements(op.RootRoot.Children, "Checkbox").ToList();
}
}
}