/ /指定された形式のオブジェクトをjsonに変換する-contd - json、c#-4.0、json.net

指定された形式のオブジェクトをjsonに変換する-contd - json、c#-4.0、json.net

この質問は、 この..、それを変更するよりも、私はここに新しいものを求めています。 私の必要なjson形式は

{
"nodes": {
"1": {
"2": {
"attriba": "a2",
"label": "2",
"attribc": false
},
"3": {
"attriba": "a3",
"label": "3",
"attribc": false
}
},
"6": {
"4": {
"attriba": "none",
"label": "4",
"attribc": false
},
"5": {
"attriba": "none",
"label": "5",
"attribc": false
}
}
}
}

今では通常、クラスを作成し、それらにデータを入力し、 "Newtonsoft.Json.JsonConvert.SerializeObject"を呼び出して目的のJSON文字列を取得します。

しかしこの場合、フォーマットはクラス構造を理解することができないほどです。

私の最後の質問ごとのトップクラスは、次のようになります..

public class Response
{
[JsonProperty("nodes")]
public Dictionary<string, Node> Nodes { get; set; }

}

下級クラス..

public class Nodedata
{

[JsonProperty("attriba")]
public string Attriba { get; set; }

[JsonProperty("attribb")]
public string Attribb { get; set; }

[JsonProperty("label")]
public string Label { get; set; }

[JsonProperty("attribc")]
public bool Attribc { get; set; }
}

しかし、キー値がなく、Nodedataオブジェクトのリストを持つノードクラス(値 "1"& "6")をどうやって管理するのですか?

どんな援助も誠実に評価されます..

ありがとう

回答:

回答№1は0

ノード1と6のクラスは型になります Dictionary<string, Object>

2,3,4,5のあなたのクラスはあなたがそれを設計したときのものになります。

あなたは何かのようなものを得る

BottomNode node1 = new BottomNode("a2", 2, false);
BottomNode node2 = new BottomNode("a3", 3, false);
BottomNode node3 = new BottomNode("none", 4, false);
BottomNode node4 = new BottomNode("none", 5, false);

Dictionary<string, Object> dic1 = new Dictionary<string, Object>();
Dictionary<string, Object> dic6 = new Dictionary<string, Object>();

dic1.Add("2", node1);
dic1.Add("3", node2);
dic6.Add("4", node3);
dic6.Add("5", node4);

Dictionary<string, Object> nodes = new Dictionary<string, Object>();

nodes.Add("1", dic1);
nodes.Add("6", dic6);