我正在尝试从我的c#应用程序中的JSON文件中读取配置。该场景是这样的,前缀将从配置中读取,后缀将在读取时动态追加。
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);
提前感谢!
将GetIterationSuffix设置为静态和内部的,然后执行以下操作:
var scenarioDes = JsonConvert.DeserializeObject<AppConfig>(jsonstring);
scenarioDes.Iteration = GetIterationSuffix();