创建树视图动态地根据的WinForms JSON文本 [英] Creating tree view dynamically according to json text in Winforms

查看:153
本文介绍了创建树视图动态地根据的WinForms JSON文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立来自外部源运行时得到的JSON消息的应用程序。



我不知道消息文本的任何结构。



我想借此JSON文本,它呈现为树视图(或其它等值,UI方面),
编辑本JSON在树视图中,我只是动态创建和发送文本回源。



我真的不知道从哪里start..Any建议?


解决方案

 私人无效btn_Convert_MouseClick(对象发件人,MouseEventArgs E)
{

{
JSON字符串= rbt_display.Text;
JObject OBJ = JObject.Parse(JSON);
tvw_display.Nodes.Clear();
树节点父= Json2Tree(OBJ);
parent.Text =根对象;
tvw_display.Nodes.Add(父);

}
赶上(异常前)
{
MessageBox.Show(ex.Message,ERROR);
}
}
私人树节点Json2Tree(JObject OBJ)
{
//创建父节点
树节点父=新的TreeNode();
//通过obj的循环。所有令牌应该是对<关键字,值>
的foreach(在物镜变种令牌)
{
//更改父$ B $的显示内容B parent.Text = token.Key.ToString();
//创建子节点
TreeNode的孩子=新的TreeNode();
child.Text = token.Key.ToString(); //检查
如果该值类型的OBJ召回方法
如果(token.Value.Type.ToString()==对象)
{
//子的.text = token.Key.ToString();
//创建一个使用的Token.value
JObject O =(JObject)token.Value新JObject;
//召回方法
=子Json2Tree(O);
//孩子添加到parentNode
parent.Nodes.Add(小孩);
}
//如果类型数组
的否则,如果(token.Value.Type.ToString()==阵列)
{
INT九= -1;
// child.Text = token.Key.ToString();
//循环虽然阵列
的foreach(在token.Value VAR ITM)
{
//检查(ITM如果值是对象
数组。 Type.ToString()==对象)
{
树节点objTN =新的TreeNode();
//child.Text = token.Key.ToString();
//回调方法
九++;

JObject O =(JObject)ITM;
objTN = Json2Tree(O);
objTN.Text = token.Key.ToString()+[+ IX +];
child.Nodes.Add(objTN);
//parent.Nodes.Add(child);
}
//规则排列字符串,整数,等
,否则如果(itm.Type.ToString()==阵列)
{
九++;
树节点dataArray中=新的TreeNode();
的foreach(ITM中的数据VAR)
{
dataArray.Text = token.Key.ToString()+[+ IX +];
dataArray.Nodes.Add(data.ToString());
}
child.Nodes.Add(dataArray中);
}

,否则
{
child.Nodes.Add(itm.ToString());
}
}
parent.Nodes.Add(小孩);
}
,否则
{
//如果token.Value不是嵌套
// child.Text = token.Key.ToString();
//改变数值为N / A,如果值== null或空字符串
如果(token.Value.ToString()==)
child.Nodes.Add( N / A);
,否则
child.Nodes.Add(token.Value.ToString());
parent.Nodes.Add(小孩);
}
}
回报父母;

}
样品JSON
{
名字:约翰,
姓氏:史密斯,
的isAlive :真实,
时代:25,
height_cm:167.6,
地址:{
的StreetAddress:21街2号,
城市:纽约,
状态:NY,
邮编:10021-3100
},
PHONENUMBERS:[
{
类型:家,
号:212 555-1234
},
{
类型: 办公室,
号:646 555-4567
}
]
孩子:[],
配偶:空
}




请注意:本例使用NewtonSoft JSON。右键单击解决方案,然后单击管理的NuGet包安装的参考。



I am building an application that gets in run-time JSON message from external source.

I don't know anything about the structure of the message text.

I want to take this JSON text, render it to a tree view (or something equivalent, UI regarding), edit this JSON in that tree view that I just dynamically created, and send the text back to the source.

I really don't know where to start..Any suggestions?

解决方案

 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.

这篇关于创建树视图动态地根据的WinForms JSON文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆