我正在尝试从我的C#应用程序中的JSON文件中读取配置。这种情况是从配置中读取前缀,并在读取时动态附加后缀。
config.json
{
"Scenarios": [
{
"Name": "Flights",
"IterationPrefix": "TeamIteration\\ABC\\123\\",
"ParentLinkId": 123
},
{
"Name": "Parcels",
"IterationPrefix": "TeamIteration\\DEF\\234\\",
"ParentLinkId": 234
},
{
"Name": "Cars",
"IterationPrefix": "TeamIteration\\FEG\\456\\",
"ParentLinkId": 456
}
]
}
对象映射如下 的 ConfigMap.cs
public class AppConfig
{
public Scenario[] Scenarios;
}
public class Scenario
{
public string Name { get; set; }
public string Iteration { get; set; }
public string ParentLinkId { get; set; }
private string GetIterationSuffix()
{
var quarters = new[] { 3, 3, 3, 4, 4, 4, 1, 1, 1, 2, 2, 2 };
var currentDate = DateTime.Now;
var month = currentDate.ToString("MMMM");
var year = currentDate.Month <= 6 ? currentDate.Year : (currentDate.Year + 1);
var quarter = quarters[currentDate.Month - 1];
return "FY" + year + "\\Q" + quarter + "\\" + month;
}
}
我希望Iteration
为IterationPrefix + GetIterationSuffix()
。不确定如何才能实现。
我试图以下列方式反序列化
JsonConvert.DeserializeObject<AppConfig>(jsonstring);
提前致谢!
答案 0 :(得分:0)
将GetIterationSuffix设为静态和内部,然后执行以下操作:
var scenarioDes = JsonConvert.DeserializeObject<AppConfig>(jsonstring);
scenarioDes.Iteration = GetIterationSuffix();