使用 C# 将树视图转换为 JSON
本文关键字:转换 JSON 视图 使用 | 更新日期: 2023-09-27 18:33:54
我必须使用 C# 将TreeView
对象转换为 JSON。我目前正在使用JsonConvert.SerializeObject()
.
public class SubTreeNode : TreeNode
{
public CustomProperties customProperties;
}
public class CustomProperties
{
public string property1 = "Property1";
public string property2 = "Property2";
public string property3 = "Property3";
}
当尝试使用它时JsonConvert.SerializeObject(treeView1.Nodes);
它只返回顶部节点......不是子节点、子节点等。
序列化和反序列化此 TreeView 对象的最简单方法是什么?
您需要添加以递归方式从每个节点生成 JSON 的方法。
查看相关文章:将树序列化为 json 对象。
我遇到了同样的问题:只有顶级节点使用标准 JSON.Net 序列化程序导出,因为正如上述链接下的fero正确指出的那样,"您的TreeNode类被序列化为数组,因为它实现了IEnumerable"。它进一步说你应该用JsonConverterAttribute装饰TreeNode类。我没有让这个工作。
这是一个替代方案。请注意,这可能不被视为最佳方法。但这很简单:
- 您使用的是标准 TreeNode 类,并且您的数据存储在 TreeView 中
- 您想使用 JSON.NET 序列化程序
使用要在 JSON 中导出的信息创建一个新的分层类(就我而言,我只想导出我的部分 treenode-属性,因此创建一个新的简单类两次有意义(:
'Simplified version of tree for json
'Compared to TreeNode class, this object is also serializable with the standard JSON.NET Serializer
Public Class JTree
Public children As New List(Of JTree)()
Private _name As String
Public Property name() As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
Private _id As String
Public Property id() As String
Get
Return _id
End Get
Set(value As String)
_id = value
End Set
End Property
End Class
递归地将数据从 TreeView 移动到新的 JTree(我们的自定义类(:
Public Sub createSimplifiedJSONTree(parentNode As TreeNode, ByRef JTreeSimp As JTree)
'Start recursion on all subnodes
For Each childNode As TreeNode In parentNode.Nodes
Dim jchild As New JTree
jchild.id = childNode.Name
jchild.name = childNode.Text
JTreeSimp.children.Add(jchild)
createSimplifiedJSONTree(childNode, jchild)
Next
End Sub
使用序列化程序将简化的 JSON 树写入文件 JSON.NET:
Private Sub WriteJSONfromTreeview()
Dim rootNode As TreeNode = TreeView1.Nodes(0)
Dim JTreeSimp As New JTree
createSimplifiedJSONTree(rootNode, JTreeSimp)
'serialize JSON directly to a file using JSON.Net Serializer
Using file__1 As StreamWriter = File.CreateText("c:'temp'test.txt")
Dim serializer As New JsonSerializer()
serializer.Formatting = Formatting.Indented
serializer.Serialize(file__1, JTreeSimp)
End Using
End Sub
最终 txt(示例(:
{
"children": [
{
"children": [
{
"children": [
{
"children": [
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "alcatraz",
"id": "021_3",
"size": 166
}
],
"name": "skyline",
"id": "031_3",
"size": 167
}
],
"name": "city",
"id": "041_5",
"size": 167
}
],
"name": "coit",
"id": "051_4",
"size": 169
}
],
"name": "tower",
"id": "061_3",
"size": 170
}
],
"name": "telegraphhill",
"id": "071_3",
"size": 170
}
],
"name": "coittower",
"id": "081_2",
"size": 170
},
{
"children": [
{
"children": [],
"name": "sunset",
"id": "071_112",
"size": 3
}
],
"name": "berkeley",
"id": "081_109",
"size": 3
},
{
"children": [
{
"children": [],
"name": "marin",
"id": "071_77",
"size": 3
}
],
"name": "marinheadlands",
"id": "081_110",
"size": 3
}
],
"name": "root",
"id": "000",
"size": 0
}
的解决方案:
- 首先,我创建一个节点类,其中包含
我想要的节点数据结构,以及一个序列化对象的 JSon 方法
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)] private class node { public node() { this.children = new List<node>(); } public node(string _value, List<node> _children = null, bool _isChecked = false) { Value = _value; isChecked = _isChecked; if (_children != null) { children = _children; } } [JsonProperty("value")] public string Value { get; set; } [JsonProperty("isChecked")] public bool isChecked { get; set; } [JsonProperty("children", NullValueHandling = NullValueHandling.Ignore)] public List<node> children { get; set; } [JsonIgnore] public string JSon { get { return JsonConvert.SerializeObject(this); } } }
我写了一个方法,它应该递归调用。这将返回给定特定树节点的子节点列表
private List<node> RunNode(TreeNode node) { List<node> nodeOut = new List<node>(); foreach(TreeNode child in node.Nodes) { List<node> grandchild = RunNode(child); nodeOut.Add(new node(child.Text, grandchild, child.Checked)); } return nodeOut; }
我编写并更新了对象方法来创建一个根节点,我可以在其中包含树视图的所有节点。我决定使用根节点而不是节点列表,因为列表没有序列化对象的 JSon 方法。
private void ActualizarMenus() { List<node> parents= new List<node>(); foreach (TreeNode node in trw.Nodes) { List<node> childs = RunNode(node); parents.Add(new node(node.Text,childs,node.Checked)); } root = new node("root", parents, true); }
根对象必须声明为单个节点
private node root;
你可以调用根的JSon方法
MessageBox.show(root.JSon());
我希望这有帮助
使用自定义 JSON 序列化程序对我有用
var settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new OrgChartConverter() },
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(tree, settings);
public class OrgChartConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Node<T>).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Node<T> node = (Node<T>)value;
JObject obj = new JObject();
obj.Add("Name", node.Value.Name);
obj.Add("Children", JArray.FromObject(node.Children, serializer));
obj.WriteTo(writer);
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}