我发现很难理解我做错了什么。我的目标是读取JSON格式化文本并将其加载到我的winforms应用程序窗口。我在树视图中使用复选框的位置。我的Json文件看起来像:
[
{
"ChildChecked": false,
"ChildTitle": null,
"NodeChecked": true,
"NodeTitle": "Parent1"
},
{
"ChildChecked": false,
"ChildTitle": null,
"NodeChecked": false,
"NodeTitle": "Parent2"
},
{
"ChildChecked": false,
"ChildTitle": null,
"NodeChecked": false,
"NodeTitle": "Parent3"
},
{
"ChildChecked": false,
"ChildTitle": "Child31",
"NodeChecked": false,
"NodeTitle": null
},
{
"ChildChecked": false,
"ChildTitle": "Child32",
"NodeChecked": false,
"NodeTitle": null
},
{
"ChildChecked": false,
"ChildTitle": "Child33",
"NodeChecked": false,
"NodeTitle": null
},
]
正如您所看到的,如果NodeTitle为null,那么这意味着该节点是子节点,并且如果nodetitle为null,则这是父节点。我的阅读和加载看起来像这样:
private void LoadConfig(string path)
{
var cfgString = File.ReadAllText(path);
var cfg = DocConfig.LoadFromString(cfgString);
foreach (var part in cfg.Parts)
{
foreach (TreeNode node in treeView1.Nodes)
{
if (part.NodeTitle == "Parent1")
{
node.Checked = part.NodeChecked;
}
if (part.NodeTitle == "Parent2")
{
node.Checked = part.NodeChecked;
}
if (part.NodeTitle == "Parent3")
{
node.Checked = part.NodeChecked;
}
foreach (TreeNode child in node.Nodes)
{
if (part.ChildTitle == "Child31")
{
child.Checked = part.ChildChecked;
}
if (part.ChildTitle == "Child32")
{
child.Checked = part.ChildChecked;
}
if (part.ChildTitle == "Child33")
{
child.Checked = part.ChildChecked;
}
如果我的json文件已加载,我的加载事件持有者正在发送消息。它说它已加载,但他没有检查任何用JSon文件编写的复选框。也许你可以帮助我?
我的保存方法:
private void SaveConfig(string path)
{
var config = new DocConfig();
config.Parts = new List<DocPart>();
foreach (TreeNode node in treeView1.Nodes)
{
{
config.Parts.Add(new DocPart { NodeTitle = node.Text, NodeChecked = node.Checked });
}
foreach (TreeNode child in node.Nodes)
{
config.Parts.Add(new DocPart { ChildTitle = child.Text, ChildChecked = child.Checked });
}
var configString = config.SaveToString();
File.WriteAllText(path, configString);
}
我的DocConfig和DocParts课程:
感谢。
public class DocPart
{
public string NodeTitle { get; set; }
public bool NodeChecked { get; set; }
public string ChildTitle { get; set; }
public bool ChildChecked { get; set; }
}
public class DocConfig
{
public List<DocPart> Parts { get; set; }
public static DocConfig LoadFromString(string jsonData)
{
var serializer = new DataContractJsonSerializer(typeof(DocConfig));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
var config = (DocConfig)serializer.ReadObject(ms);
return config;
}
public string SaveToString()
{
var serializer = new DataContractJsonSerializer(typeof(DocConfig));
var ms = new MemoryStream();
serializer.WriteObject(ms, this);
return Encoding.UTF8.GetString(ms.ToArray()).PrettyPrintJson();
}
}
答案 0 :(得分:2)
描述树结构的数据是List<DataNode
&gt;。 TreeRepository
类将json文件加载到TreeView
并将TreeView
数据结构保存到json文件中:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JsonTreeView
{
public class DataNode
{
public string Text { get; set; }
public bool IsChecked { get; set; }
public List<DataNode> Children { get; set; }
}
public static class TreeRepository
{
public static List<DataNode> LoadDataNodes(string jsonFilePath)
{
var json = System.IO.File.ReadAllText(jsonFilePath);
var dataNodes = JsonConvert.DeserializeObject<List<DataNode>>(json);
return dataNodes;
}
public static void Load(this TreeView treeView, string jsonFilePath)
{
treeView.Nodes.Load(LoadDataNodes(jsonFilePath));
}
public static void Load(this TreeNodeCollection nodes, List<DataNode> dataNodes)
{
foreach(var dataNode in dataNodes)
{
var treeNode = nodes.Add(dataNode.Text);
treeNode.Checked = dataNode.IsChecked;
if (dataNode.Children != null && dataNode.Children.Count > 0)
{
Load(treeNode.Nodes, dataNode.Children);
}
}
}
public static List<DataNode> GetDataNodes(this TreeView treeView)
{
var dataNodes = new List<DataNode>();
AddNodesToList(treeView.Nodes, dataNodes);
return dataNodes;
}
private static void AddNodesToList(TreeNodeCollection nodes, List<DataNode> dataNodes)
{
foreach (TreeNode node in nodes)
{
var dataNode = new DataNode
{
Children = new List<DataNode>(),
IsChecked = node.Checked,
Text = node.Text
};
dataNodes.Add(dataNode);
if (node.Nodes.Count > 0)
{
AddNodesToList(node.Nodes, dataNode.Children);
}
}
}
public static void Save(this TreeView treeView, string jsonFilePath)
{
var dataNodes = treeView.GetDataNodes();
var json = JsonConvert.SerializeObject(dataNodes, Formatting.Indented);
System.IO.File.WriteAllText(jsonFilePath, json);
}
}
}
表单有一个树形视图,有三个按钮,代码如下:
using System;
using System.Windows.Forms;
namespace JsonTreeView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string jsonFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TreeView.json");
private void saveButton_Click(object sender, EventArgs e)
{
this.treeView1.Save(jsonFilePath);
}
private void clearButton_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Clear();
}
private void loadButton_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Clear();
this.treeView1.Load(jsonFilePath);
}
}
}
这就是TreeView.json
文件的样子:
[
{
"Text": "Item 1",
"IsChecked": false,
"Children": [
{
"Text": "Item 1.1",
"IsChecked": true,
"Children": []
},
{
"Text": "Item 1.2",
"IsChecked": false,
"Children": [
{
"Text": "Item 1.2.1",
"IsChecked": true,
"Children": []
}
]
}
]
},
{
"Text": "Item 2",
"IsChecked": true,
"Children": [
{
"Text": "Item 2.1",
"IsChecked": false,
"Children": []
},
{
"Text": "Item 2.2",
"IsChecked": true,
"Children": [
{
"Text": "Item 2.2.1",
"IsChecked": false,
"Children": []
},
{
"Text": "Item 2.2.2",
"IsChecked": false,
"Children": []
},
{
"Text": "Item 2.2.3",
"IsChecked": true,
"Children": []
}
]
},
{
"Text": "Item 2.3",
"IsChecked": false,
"Children": []
}
]
}
]
对于此树视图: