最近工作遇到需要把JSON转化成TreeView,网上找了半天没发现合适的类,就自己写了一个,Freamwork 4.0,4.0.3下编译通过。
用到的类有:
System;
System.Linq;
System.Collections.Generic;
System.Windows.Forms;
下面是代码:
/// <summary>
/// 用途:用于把Json串转化为Tree
/// 输入:Json字串
/// 输出:TreeNode
/// 作者:BigBen2013
/// 创建时间:2013.11.10
/// </summary>
public class JsonToTree
{
private bool InSingle; //单引号环境中标志
private bool InDouble; //双引号环境中标志
private bool WaitSecondSingle; //等待单引号标志
private bool AfterBackSlash; //转义环境标志
private bool AfterColon; //冒号环境标志
private string LogMessage;
private string Buffer;
TreeNode NowNode;
Stack<TreeNode> MainStack;
Stack<char> OperatorStack;
Stack<TreeNode> ReverseStack; //用于修正每个子节点的顺序
public JsonToTree(string LogMessage)
{
this.LogMessage = LogMessage;
InSingle = false;
InDouble = false;
WaitSecondSingle = false;
AfterBackSlash = false;
AfterColon = false;
Buffer = string.Empty;
MainStack = new Stack<TreeNode>();
OperatorStack = new Stack<char>();
ReverseStack = new Stack<TreeNode>();
}
//JSON转化为Tree主方法
public Tr