这些是我的课程
public class Competitor
{
public string Team { get; set; }
public string Win { get; set; }
}
public class CompetitorsClass
{
public int ActiveCompetitors { get; set; }
public List<Competitor> Competitors { get; set; }
public int TotalCompetitors { get; set; }
public bool HasWinOdds { get; set; }
}
public class RootObject
{
public int EventID { get; set; }
public int ParentEventID { get; set; }
public string MainEvent { get; set; }
public CompetitorsClass Competitors { get; set; }
public string EventStatus { get; set; }
public bool IsSuspended { get; set; }
public bool AllowBets { get; set; }
}
这里我将数据传递给Root类。如何将值传递给CompetitorsClass竞争对手。任何人都可以告诉我该怎么做以及任何与此相似的例子?我知道这对你们大多数人来说可能是基本的东西,但我是C#和学习新手。
var obj2 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, RootObject>>>(json);
foreach (var root in obj2)
{
int count = root.Value.Count;
foreach (var child in root.Value)
{
RootObject ob = new RootObject();
ob.EventID = child.Value.EventID;
ob.EventStatus = child.Value.EventStatus;
CompetitorsClass cs = new CompetitorsClass();
cs.ActiveCompetitors = 0;
cs.HasWinOdds = false;
cs.TotalCompetitors = 2;
// cs.Competitors
// cs.Competitors = ob.Competitors;
}
}
答案 0 :(得分:0)
RootObject
具有Competitors
个属性,其类型为CompetitorsClass
。
您可以使用该属性:
cs.Competitors = ob.Competitors.Competitors;
答案 1 :(得分:0)
有两种方法可以解决这个问题:
只需指定集合,例如:
CompetitorsClass cs = new CompetitorsClass();
cs.ActiveCompetitors = 0;
cs.Competitors = myList; //or new List<Competitor>() if you don't have it yet.
传入构造函数并指定:
public class CompetitorsClass
{
public CompetitorsClass(List<Competitor> competitors)
{
Competitors = competitors;
}
...
}
cs = new CompetitorsClass(myList);
第二种通常是首选,因为它保证变量已初始化,而不是依赖于默认值(在这种情况下为null
)。
要访问现有对象的竞争对手列表,您可以写:
ob.Competitors.Competitors //This is "myList" in my examples