使用IEnumerable<;将Json转换为C#类&燃气轮机;
我有一个Json,看起来像这样使用IEnumerable<;将Json转换为C#类&燃气轮机;,c#,C#,我有一个Json,看起来像这样 { "steps": [ { "stepsType": "runWizard", "wizardType": "cv2.0Server", "config": { "mode": "add", "resourceNam
{
"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
}
}
]
}
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; }
}
}
我创建了一个这样的模型
{
"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
}
}
]
}
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间Microsoft.Database.Configuration.Model
{
公共类ClusterConfigurationModel
{
公共IEnumerable步骤{get;}
}
公共类ClusterConfigurationModelStep:ClusterConfigurationModel
{
公共字符串步骤类型{get;}
公共字符串向导类型{get;}
公共IEnumerable配置{get;}
}
公共类ClusterConfigurationServerConfig:ClusterConfigurationModelStep
{
公共字符串模式{get;}
公共字符串ResourceName{get;}
public IEnumerable ActiveDbPrimaryServer{get;}
公共字符串ActiveDbCatalog{get;}
公共字符串ActiveDbUserId{get;}
公共字符串ActiveDbPassword{get;}
公共字符串ActiveIntegratedSecurity{get;}
}
公共类ClusterConfigurationDbPrimaryServer:ClusterConfigurationServerConfig
{
公共字符串ServerName{get;}
公共字符串服务器地址{get;}
}
}
这是否正确?我正在尝试使用
我不熟悉C#,只是想知道我创建的模型是否正确,或者我是否需要修改?您应该避免使用
IEnumerable
作为字段和属性,而是使用T[]
数组或IList
这是因为枚举是一个包含state的interator,它包含指向当前项的引用。这意味着只能使用一次,然后不能再次枚举内容
相反,您通常希望创建一个列表,然后在代码中的多个位置多次枚举该列表。首先,您不需要对每个类都进行继承。例如(删除继承后):
公共类ClusterConfigurationModelStep:ClusterConfigurationModel
{
公共字符串步骤类型{get;}
公共字符串向导类型{get;}
公共IEnumerable配置{get;}
}
其次,您可能需要使用
Newtonsoft.json
中的[JsonProperty(“json\u key\u name”)]
或System.Text.json中的[JsonPropertyName(“json\u key\u name”)]
将json关键字字段名称映射到类的变量。默认情况下,在C#中使用camelCase。将JSON粘贴到。使用该模型。我使用了,但它只支持列表或数组,我想从IEnumerable的角度询问…将其更改为IEnumerable
-它是否仍然有效(可能会)?我的观点是,您不应该需要AbstractJsonConverter
等。列表和数组是IEnumerable的。他们可以使用“任何你看到IEnumerable标志的地方”欢迎!无论你是C#新手还是老手,尽你所能利用一切帮助——QuickType非常出色,可以节省大量时间进行单调乏味的POCO创建。我不同意这一点。您是否对枚举数感到困惑?一般来说,您不想使用IList
(因为它具有很好的特性,您可以添加到它,也可以不添加)IReadOnlyList
可能是这里OP的最佳选择。另请参见我同意@CaiusJard,特别是因为IEnumberable variable=new T[n]
是完全有效的。当然,您可以多次枚举IEnumerable,每次调用都会创建一个指向集合头部的新枚举器。不,我不会对枚举器
感到困惑,是的IEnumerable
是完全有效的语法。但是,它可能会产生不明显的副作用,因此我建议使用IList
或IReadOnlyList
,因为它通常更接近目的。另请参见newtonsoft和system json了解c#是PascalCase,json是camelCase。为了能够解析“someProperty”->someProperty,它们不需要(通过属性)指示这种差异。没有任何迹象表明此模型正在序列化