将对象转换为特定格式的json [英] Converting an object into json in specific format

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

问题描述

我所需的json格式是

My required json format is

{
  "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字符串.

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 I think would be like the following..

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

但是,我该如何管理没有键值的节点类(值"1"至"5").

But, how do i manage the node class( values "1" to "5") which has no key value..

我们将不胜感激任何帮助.

Any help will be sincerely appreciated..

谢谢

推荐答案

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

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

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