将对象转换为指定格式的json [英] Converting an object to json in specified format-contd

查看:446
本文介绍了将对象转换为指定格式的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是这个的延续。 ,而不是改变它,我在这里请求一个新的。
我需要的json格式是

  { 
nodes:{
1:{
2:{
attriba:a2,
label:2 ,
attribc:false
},
3:{
attriba:a3,
label:3 b $ battribc: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>节点{get;组; }

}

底层..

  public class Nodedata 
{

[JsonProperty(attriba)]
public string Attriba {得到;组; }

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

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

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

但是,如何管理节点类(值1 ;6),它再次没有键值,并具有Nodedata对象的列表。



任何帮助将真诚地赞赏..



感谢

解决方案

节点类1和6的类型词典< string,Object>



而你的类2,3,4和5将按照你的设计。 p>

你会得到像

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

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

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

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

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


This question is a continuation of this.., rather than changing it, I'm asking a new one here.. My required json format is

{
    "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
            }
        }
    }
}

Now normally I would create classes and fill them with data and call "Newtonsoft.Json.JsonConvert.SerializeObject" to get the desired json string.

But in this case the format is such that I'm unable to figure out the class structure..

The top class as per my last question would be like the following..

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

     }

The bottom class ..

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

But, how do i manage the node class( values "1" & "6") which has again no key value and has a list of Nodedata objects..

Any help will be sincerely appreciated..

Thanks

解决方案

The class for nodes, 1 and 6 will be of type Dictionary<string, Object>

and your class for 2,3,4 and 5 will be as you designed it.

You shoudl get something like

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

这篇关于将对象转换为指定格式的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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