提问者:小点点

创建复杂的多个动态对象以创建嵌套json


刚开始创建json,但需要准备一些数据发送到API,因此需要时间来学习。

这就是我想要创造的;

[
  {
    "alias": "H02010",
    "address": "Demoroad 9",
    "city": "Demotown",
    "customer_id": "727",
    "property_id": "02010",
    "services": [
      {
        "description": "Service1",
        "shortname": "S1",
        "orderdates": [
          "2020-11-30",
          "2020-12-14",
          "2020-12-28"
        ]
      },
      {
        "description": "Service2",
        "shortname": "S2",
        "orderdates": [
          "2020-11-19",
          "2020-12-17"
        ]
      }
    ]
  }
  ]

挑战在于,需要生成的服务可能介于1到10个之间。为了创建上面的“静态”版本,我做了这个;

        public class Anlobject 
        {
            public string alias { get; set; }
            public string address { get; set; }
            public string city { get; set; }
            public string customer_id { get; set; }
            public string property_id { get; set; }
            public Services services { get; set; }
            public Services2 services2 { get; set; }

        }
        public class Services
        {
            public string description { get; set; }
            public string shortname { get; set; }
            public string[] dates { get; set; }
        }
        public class Services2
        {
            public string description { get; set; }
            public string shortname { get; set; }
            public string[] dates { get; set; }
        }

    private static Anlobject CreateAnlobject()
        {
            var obj = new Anlobject()
            {
                alias = "H02010",
                address = "Demoroad 9",
                city = "Demotown",
                customer_id = "727",
                property_id = "02010",
                services = new Services()
                {
                    description = "Service1",
                    shortname = "S1",
                    orderdates = new string[] { "2020-01-01","2020-02-01"},
                },
                services2 = new Services2()
                {
                    description = "Service2",
                    shortname = "S2",
                    orderdates = new string[] { "2020-01-01", "2020-02-01" },
                },
            };
            return obj;
        }

        private void bTest_Click(object sender, EventArgs e)
        {
            var obj = CreateAnlobject();
            var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
            File.WriteAllText("c:\\temp\\jSonTestTK.json", json.ToString());
        }

有没有一种方法可以只使用一个Services(),但是调用它几次,或者我完全走错了路?

提前感谢您的任何提示,在哪里寻找或搜索。


共2个答案

匿名用户

您不需要(或希望)类或属性。属性是对象的集合。(不过我建议将类的名称改为。)

该属性如下所示:

public IEnumerable<Services> services { get; set; }

然后像这样初始化它:

services = new List<Services>
{
    new Services()
    {
        description = "Service1",
        shortname = "S1",
        orderdates = new string[] { "2020-01-01","2020-02-01"},
    },
    new Services()
    {
        description = "Service2",
        shortname = "S2",
        orderdates = new string[] { "2020-01-01", "2020-02-01" },
    }
}

匿名用户

下面是示例代码。

模型如下所示。你不需要服务2

public class Service //create class as singular
{
    public string description { get; set; }
    public string shortname { get; set; }
    public string[] orderdates { get; set; }
}

public class Anlobject
{
    public string alias { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string customer_id { get; set; }
    public string property_id { get; set; }
    public List<Service> services { get; set; }
}

函数

private static Anlobject CreateAnlobject()
{
    var service1 = new Service()
    {
        description = "Service1",
        shortname = "S1",
        orderdates = new string[] { "2020-01-01", "2020-02-01" },
    };
    var service2 = new Service()
    {
        description = "Service2",
        shortname = "S2",
        orderdates = new string[] { "2020-01-01", "2020-02-01" },
    };

    var obj = new Anlobject()
    {
        alias = "H02010",
        address = "Demoroad 9",
        city = "Demotown",
        customer_id = "727",
        property_id = "02010",
        services = new List<Service> { service1, service2}
    };
    return obj;
}

从主方法调用该方法

var obj = CreateAnlobject();
var json = JsonConvert.SerializeObject(new List<Anlobject> { obj }); //you need list of Anlobject
File.WriteAllText("c:\\rnd\\jSonTestTK.json", json.ToString());