c# - Winforms中根据json文本动态创建 TreeView

标签 c# json winforms treeview

我正在构建一个从外部源获取运行时 JSON 消息的应用程序。

我对消息文本的结构一无所知。

我想获取此 JSON 文本,将其呈现为 TreeView (或与 UI 有关的等效内容), 在我刚刚动态创建的 TreeView 中编辑此 JSON,并将文本发送回源。

我真的不知道从哪里开始..有什么建议吗?

最佳答案

 private void btn_Convert_MouseClick(object sender, MouseEventArgs e)
    {
        try
        {
            string json = rbt_display.Text;
            JObject obj = JObject.Parse(json);
            tvw_display.Nodes.Clear();
            TreeNode parent = Json2Tree(obj);
            parent.Text = "Root Object";
            tvw_display.Nodes.Add(parent);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "ERROR");
        }
    }
private TreeNode Json2Tree(JObject obj)
    {
        //create the parent node
        TreeNode parent = new TreeNode();
        //loop through the obj. all token should be pair<key, value>
        foreach (var token in obj)
        {
            //change the display Content of the parent
            parent.Text = token.Key.ToString();
            //create the child node
            TreeNode child = new TreeNode();
            child.Text = token.Key.ToString();
            //check if the value is of type obj recall the method
            if (token.Value.Type.ToString() == "Object")
            {
               // child.Text = token.Key.ToString();
                //create a new JObject using the the Token.value
                JObject o = (JObject)token.Value;
                //recall the method
                child = Json2Tree(o);
                //add the child to the parentNode
                parent.Nodes.Add(child);
            }
            //if type is of array
            else if (token.Value.Type.ToString() == "Array")
            {
                int ix = -1;
              //  child.Text = token.Key.ToString();
                //loop though the array
                foreach (var itm in token.Value)
                {
                    //check if value is an Array of objects
                    if (itm.Type.ToString() == "Object")
                    {
                        TreeNode objTN = new TreeNode();
                        //child.Text = token.Key.ToString();
                        //call back the method
                        ix++;

                        JObject o = (JObject)itm;
                        objTN = Json2Tree(o);
                        objTN.Text = token.Key.ToString() + "[" + ix + "]";
                        child.Nodes.Add(objTN);
                        //parent.Nodes.Add(child);
                    }
                    //regular array string, int, etc
                    else if(itm.Type.ToString() == "Array")
                    {
                        ix++;
                        TreeNode dataArray = new TreeNode(); 
                        foreach (var data in itm)
                        {
                            dataArray.Text = token.Key.ToString() + "[" + ix + "]";
                            dataArray.Nodes.Add(data.ToString());
                        }
                        child.Nodes.Add(dataArray);   
                    }

                    else
                    {
                        child.Nodes.Add(itm.ToString());
                    }
                }
                parent.Nodes.Add(child);
            }
            else
            {
                //if token.Value is not nested
               // child.Text = token.Key.ToString();
                //change the value into N/A if value == null or an empty string 
                if (token.Value.ToString() == "")
                    child.Nodes.Add("N/A");
                else
                    child.Nodes.Add(token.Value.ToString());
                parent.Nodes.Add(child);
            }
        }
        return parent;

    }
sample json
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"height_cm": 167.6,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
  "type": "home",
  "number": "212 555-1234"
},
{
  "type": "office",
  "number": "646 555-4567"
}
],
"children": [],
"spouse": null
}

Note: This example uses NewtonSoft Json. Right-click solution and click manage NuGet packages to install the reference.

关于c# - Winforms中根据json文本动态创建 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18769634/

相关文章:

c# - 我是否需要在以下 C# 代码中使用锁定?

C# - 值类型的引用包装器

vb.net - 将图标从主窗体继承到vb.net中的子窗体

c# - 如何从计算机生成唯一 key 以许可我的应用程序?

C# Form_Load 事件方法

c# - 如何将数据源绑定(bind)到 List<Dictionary<string, string>>?

c# - 从文本框中输入的字符串中读取所有字符,不重复并计算每个字符

json - 自定义 JSON 格式 webhook WooCommerce Wordpress

json - jq 日期和 unix 时间戳

javascript - 获取 Json 文件时出错