将对象转换为特定格式的json

时间:2014-08-07 10:47:10

标签: c# json json.net

我需要的json格式是

{
  "nodes": {
    "1": {
      "attriba": "a1",
      "attribb": "b1",
      "label": "1",
      "attribc": false
    },
    "2": {
      "attriba": "a2",
      "label": "2",
      "attribc": false
    },
    "3": {
      "attriba": "a3",
      "label": "3",
      "attribc": false
    },
    "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 List<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; }
    }

但是,我如何管理没有键值的节点类(值&#34; 1&#34;到&#34; 5&#34;)。

真诚地感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

public class Response
{
    public Dictionary<string, Node> nodes {get;set;}
}

public class Node
{
    public string attriba { get; set; }
    public string attribb { get; set; }
    public string label { get; set; }
    public bool attribc { get; set; }
}
相关问题