精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。
json文本虽然节省空间,但是看起来不直观,转换成树形就直观,容易理解。虽然网页上有解析工具,用起来还是不太方便,这里翻译的是一个C#的json树形显示器的介绍,欢迎阅读。里面用了原始的方式,没有用集成好的json解析类,对初学者学习有用。
As an web developer I'm almost always working with JSON data. Most of the time I can look at the data and get a good understanding of whats going and if needed I just run it through a JSON decode script usually in PHP. But recently, I was given a large JSON file which was initially an Ajax response from a website. Normally I would just write a simple script to make the data more readable but after working with the JavaScriptSerializer class in a recent .Net web application, I wondered how difficult it would be to develop a windows application that could decode JSON data and present it to me in a more meaningful way. 作为Web开发人员,我几乎总是使用JSON数据。大多数时候,我可以查看数据并深入了解最新情况,如果需要,我只需通过PHP中的JSON解码脚本运行它。但最近,我获得了一个大型JSON文件,该文件最初是来自网站的Ajax响应。通常我会编写一个简单的脚本来使数据更具可读性但是在最近的.Net Web应用程序中使用JavaScriptSerializer类之后,我想体验下困难,进行开发一个可以解码JSON数据并以更有意义的方式将其呈现它的Windows应用程序。
It's been a while since I worked on a Windows Forms application and immediately I ran into a problem. For some reason, I was not able to add a reference to System.Web.Extensions. The Add Reference dialog box didn't list the reference. After some time of head banging and frustration, I realized it was to do with the target framework. My application was targeting the .Net Framework 4 Client Profile. I switched it to use .Net Framework 4 and checked the reference list again. From this moment on, the head banging stopped.自从我在Windows Forms应用程序上工作已经有一段时间了,我立即遇到了问题。出于某种原因,我无法添加对System.Web.Extensions的引用。“添加引用”对话框未列出引用。经过一段时间的头疼和挫折之后,我意识到这与目标框架有关。我的应用程序针对的是.Net Framework 4 Client Profile。我将其切换为使用.Net Framework 4并再次检查参考列表。从这一刻起,困惑没了。
Now that I was able to add the reference I could use the System.Web.Script.Serialization namespace. Within this namespace is a class called JavaScriptSerializer. It has a Deserialize method, which can deserialize the JSON data to a Type. I used a Dictionary type since JSON can be considered to be a key/value pair where value can also be an array. Listing 1.1 below shows an excerpt of the deserializing process from the sample application.现在我能够添加引用,我可以使用System.Web.Script.Serialization命名空间。在此命名空间中有一个名为JavaScriptSerializer的类。它有一个Deserialize方法,可以将JSON数据反序列化为Type。我使用了Dictionary类型,因为JSON可以被认为是一个键/值对,其中value也可以是一个数组。下面的清单1.1显示了示例应用程序中反序列化过程的摘录。
Listing 1.1
JavaScriptSerializer js = new JavaScriptSerializer();
try
{
Dictionary<string,> dic = js.Deserialize<dictionary<string,>>(txtInput.Text);
TreeNode rootNode = new TreeNode("Root");
jsonExplorer.Nodes.Add(rootNode);
BuildTree(dic, rootNode);
}
catch (ArgumentException argE)
{
MessageBox.Show("JSON data is not valid");
}
Once I had deserialized the data, I needed a way to show it. For this I chose to use a TreeView control where a TreeNode could represent both a key and a value. Notice in the code about I have a method BuildTree(), which accepts a Dictionary object and a TreeNode. This method recursively loops through each item and checks if the value is of Type Dictionary and if it is, it passes the new Dictionary object to it self (BuildTree) with a parent TreeNode.一旦我对数据进行了反序列化,我就需要一种方法来显示它。为此,我选择使用TreeView控件,其中TreeNode可以表示键和值。在代码中注意我有一个方法BuildTree(),它接受一个Dictionary对象和一个TreeNode。此方法递归循环遍历每个项目并检查该值是否为Type Dictionary,如果是,则将新的Dictionary对象传递给self(BuildTree)并使用父TreeNode。
If the value is not of Type Dictionary then I check if it is of Type ArrayList and if it is, then I loop through the items and add them as the final TreeNode. Finally, if the value if not of either Dictionary or ArrayList Types then I assume it is a string and create a final TreeNode. I gave a blue text color to each final TreeNode value to indicate it was not a parent TreeNode.如果该值不是Type Dictionary,那么我检查它是否是Type ArrayList,如果是,那么我遍历这些项并将它们添加为最终的TreeNode。最后,如果值不是Dictionary或ArrayList Types,那么我假设它是一个字符串并创建一个最终的TreeNode。我为每个最终的TreeNode值指定了蓝色文本颜色,以指示它不是父TreeNode。
Listing 1.2 below shows the complete BuildTree method.下面的清单1.2显示了完整的BuildTree方法。
Listing 1.2
public void BuildTree(Dictionary<string,> dictionary, TreeNode node)
{
foreach (KeyValuePair<string,> item in dictionary)
{
TreeNode parentNode = new TreeNode(item.Key);
node.Nodes.Add(parentNode);
try
{
dictionary = (Dictionary<string,>)item.Value;
BuildTree(dictionary, parentNode);
}
catch (InvalidCastException dicE) {
try
{
ArrayList list = (ArrayList)item.Value;
foreach (string value in list)
{
TreeNode finalNode = new TreeNode(value);
finalNode.ForeColor = Color.Blue;
parentNode.Nodes.Add(finalNode);
}
}
catch (InvalidCastException ex)
{
TreeNode finalNode = new TreeNode(item.Value.ToString());
finalNode.ForeColor = Color.Blue;
parentNode.Nodes.Add(finalNode);
}
}
}
}
The rest of the application is made up of reading a file with the help of the OpenFileDialog class and the System.IO namespace.应用程序的其余部分包含读取文件,这个功能涉及到OpenFileDialog类的帮助类和System.IO命名空间。