4

外部ソースからランタイム JSON メッセージを取得するアプリケーションを構築しています。

メッセージテキストの構造については何も知りません。

この JSON テキストを取得し、それをツリー ビュー (または同等の UI に関するもの) にレンダリングし、動的に作成したツリー ビューでこの JSON を編集し、テキストをソースに送り返します。

どこから始めればいいのか本当にわかりません.何か提案はありますか?

4

5 に答える 5

14
 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
}

注: この例では、NewtonSoft Json を使用しています。ソリューションを右クリックし、[NuGet パッケージの管理] をクリックして参照をインストールします。

于 2015-03-25T15:52:34.533 に答える
0

本当にたくさんの質問があります。そのすべての部分について本当にガイダンスが必要な場合は、ここで試して回答するのは大変です.

JSON 構造を読み取るためのクラスがあり、すぐに利用できます。Yosiが間接的にリンクしているように、JSON.netがあります

JSON を読み取ることができたら、それを使用してTreeViewを構築できます。

にはインプレース編集をサポートTreeViewする のプロパティがあるため、編集は非常に簡単です。LabelEditそこからは、それに反応して変化を追跡するだけです。または、最後にすべてを一挙に読み返すこともできます。いずれにせよ、 には、などのTreeViewイベントがあり、それらはすべて上記のリンクにあります。BeforeLabelEditAfterLabelEditTreeView

于 2013-09-12T19:50:07.290 に答える