简体   繁体   English

使用 IEnumerable<> 将 Json 转换为 C# class

[英]Convert Json to C# class using IEnumerable<>

I have a Json which looks like this...我有一个 Json 看起来像这样......

{
  "steps": [
    {
      "stepsType": "runWizard",
      "wizardType": "cv2.0Server",
      "config": {
        "mode": "add",
        "resourceName": "cv2.0 Server",
        "activeDbPrimaryServer": {
          "serverName": "JJH3M005A",
          "serverAddress": "JJH3M005A.microsoft.info"
        },
        "activeDbCatalog": "cv2.0Database",
        "activeDBUserId": "user",
        "activeDBPassword": "password",
        "activeIntegratedSecurity": false
      }
    }
  ]
}

I have created a model like this...我已经创建了一个像这样的 model ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Database.Configuration.Model
{
    public class ClusterConfigurationModel
    {
        public IEnumerable<ClusterConfigurationModelStep> Steps { get; }
    }

    public class ClusterConfigurationModelStep : ClusterConfigurationModel
    {
        public string StepType { get;  }

        public string WizardType { get;  }

        public IEnumerable<ClusterConfigurationServerConfig> Config { get;  }     
    }

    public class ClusterConfigurationServerConfig : ClusterConfigurationModelStep
    {
        public string Mode { get; }

        public string ResourceName { get; }

        public IEnumerable<ClusterConfigurationDbPrimaryServer> ActiveDbPrimaryServer { get; }

        public string ActiveDbCatalog { get; }

        public string ActiveDbUserId { get; }

        public string ActiveDbPassword { get; }

        public string ActiveIntegratedSecurity { get; }
    }

    public class ClusterConfigurationDbPrimaryServer : ClusterConfigurationServerConfig
    {
        public string ServerName { get; }

        public string ServerAddress { get;  }
    }
}

Is this correct I am trying to create a custom json convertor using,这是正确的吗?我正在尝试使用创建自定义 json 转换器,

  1. AbstractJsonConverter AbstractJsonConverter
  2. JsonConverterFactory with an Interface带有接口的 JsonConverterFactory
  3. JsonConverter with an Interface带有接口的 JsonConverter

I am new to C#, just want to know whether the model I have created is correct or do I need some modification?我是 C# 的新手,只想知道我创建的 model 是否正确还是需要修改?

First of all, you need not to have inheritance for every single class.首先,您不需要为每个 class 配备 inheritance。 For example (after removing inheritance):例如(删除继承后):

public class ClusterConfigurationModelStep : ClusterConfigurationModel
{
    public string StepType { get;  }

    public string WizardType { get;  }

    public IEnumerable<ClusterConfigurationServerConfig> Config { get;  }     
}

Second, you may want to have [JsonProperty("json_key_name")] from Newtonsoft.Json or [JsonPropertyName("json_key_name")] from System.Text.Json for mapping JSON keyfield name to class's variable. Second, you may want to have [JsonProperty("json_key_name")] from Newtonsoft.Json or [JsonPropertyName("json_key_name")] from System.Text.Json for mapping JSON keyfield name to class's variable. It is by default using camelCase in C#.在 C# 中默认使用 camelCase。

You should avoid using IEnumerable<T> for fields and properties, use a T[] array or an IList<T> instead.您应该避免对字段和属性使用IEnumerable<T> ,而是使用T[]数组或IList<T>

The reason for this is that an enumerable is an interator that contains state, it contains a reference to the current item being pointed to.原因是可枚举是一个包含 state 的交互器,它包含对当前指向的项目的引用。 This means that it can only be consumed once, and then you can not enumerate the contents again.这意味着它只能被消费一次,然后你不能再次枚举内容。

Instead, you usually want to create a list instead and then enumerate that list multiple times in multiple places in your code.相反,您通常希望创建一个列表,然后在代码中的多个位置多次枚举该列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM