Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将对象转换为指定格式的json contd_Json_C# 4.0_Json.net - Fatal编程技术网

将对象转换为指定格式的json contd

将对象转换为指定格式的json contd,json,c#-4.0,json.net,Json,C# 4.0,Json.net,这个问题是…的延续,我不是在改变它,而是在问一个新问题。。 我需要的json格式是 { "nodes": { "1": { "2": { "attriba": "a2", "label": "2", "attribc": false }, "3": { "attriba":

这个问题是…的延续,我不是在改变它,而是在问一个新问题。。 我需要的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; }

     }
但是,如何管理节点类值1和6,它们同样没有键值,并且有一个Nodedata对象列表

如有任何帮助,我们将不胜感激


谢谢

节点类,1和6将是Dictionary类型

你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);

它是字典中的字典。@Jeroenvanevel那么,节点类应该是。。公共类节点{public Dictionary data{get;set;}}--并且没有JsonProperty?不,这是字典中的列表。请尝试Dictionary,而不是本例中没有列表,在列表中,对象将介于[]之间
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);