我需要JSON文件中的每个玩家名字和姓氏在数据网格视图中填充。一切都很好,除了我在dgv中只有2名玩家。这两名球员都是在json的预期部分之后,所以只有2名球员为我展示了#34; Marcus Smart"和" Cedi Osman"。
CODE:
public void StartingLineups()
{
upass.upass up = new upass.upass();
string address3 = "https://api.mysportsfeeds.com/v1.1/pull/nba/2017-2018-regular/game_startinglineup.json?gameid=" + dateTimePicker1.Text + comboBox2.Text;
var w3 = new WebClient();
w3.UseDefaultCredentials = true;
w3.Credentials = new NetworkCredential(up.username, up.password);
var result3 = w3.DownloadString(address3);
var obj3 = JObject.Parse(result3);
dt.Columns.Add("Projected Lineup", typeof(string));
StartingLineup.startinglineup sl = new StartingLineup.startinglineup();
foreach (JObject child in obj3["gamestartinglineup"]["teamLineup"])
{
sl.expected = child["expected"]["starter"][0]["player"]["FirstName"].ToString() + " " + child["expected"]["starter"][0]["player"]["LastName"].ToString();
DataRow row = dt.NewRow();
dataGridView1.DataSource = dt;
row["Projected Lineup"] = sl.expected;
dt.Rows.Add(row);
}
}
JSON:
{
"gamestartinglineup": {
"lastUpdatedOn": "2017-10-17 12:56:32 PM",
"game": {
"id": "42070",
"date": "2017-10-17",
"time": "8:00PM",
"awayTeam": {
"ID": "82",
"City": "Boston",
"Name": "Celtics",
"Abbreviation": "BOS"
},
"homeTeam": {
"ID": "86",
"City": "Cleveland",
"Name": "Cavaliers",
"Abbreviation": "CLE"
},
"location": "Quicken Loans Arena"
},
"teamLineup": [
{
"team": {
"ID": "82",
"City": "Boston",
"Name": "Celtics",
"Abbreviation": "BOS"
},
"expected": {
"starter": [
{
"position": "Bench8",
"player": {
"ID": "9102",
"LastName": "Smart",
"FirstName": "Marcus",
"JerseyNumber": "36",
"Position": "PG"
}
},
{
"position": "Starter1",
"player": {
"ID": "9506",
"LastName": "Hayward",
"FirstName": "Gordon",
"JerseyNumber": "20",
"Position": "SF"
}
},
{
"position": "Starter2",
"player": {
"ID": "10090",
"LastName": "Brown",
"FirstName": "Jaylen",
"JerseyNumber": "7",
"Position": "SF"
}
},
{
"position": "Starter5",
"player": {
"ID": "9082",
"LastName": "Horford",
"FirstName": "Al",
"JerseyNumber": "15",
"Position": "C"
}
},
{
"position": "Starter3",
"player": {
"ID": "9157",
"LastName": "Irving",
"FirstName": "Kyrie",
"JerseyNumber": "11",
"Position": "PG"
}
},
{
"position": "Starter4",
"player": {
"ID": "13730",
"LastName": "Tatum",
"FirstName": "Jayson",
"JerseyNumber": "0",
"Position": "F"
}
},
{
"position": "Bench1",
"player": {
"ID": "13775",
"LastName": "Ojeleye",
"FirstName": "Semi",
"JerseyNumber": "37",
"Position": "PF"
}
},
{
"position": "Bench2",
"player": {
"ID": "13778",
"LastName": "Yabusele",
"FirstName": "Guerschon",
"JerseyNumber": "30",
"Position": "PF"
}
},
{
"position": "Bench3",
"player": {
"ID": "13770",
"LastName": "Nader",
"FirstName": "Abdel",
"JerseyNumber": "28",
"Position": "SF"
}
},
{
"position": "Bench4",
"player": {
"ID": "9105",
"LastName": "Rozier",
"FirstName": "Terry",
"JerseyNumber": "12",
"Position": "PG"
}
},
{
"position": "Bench5",
"player": {
"ID": "9211",
"LastName": "Baynes",
"FirstName": "Aron",
"JerseyNumber": "12",
"Position": "C"
}
},
{
"position": "Bench6",
"player": {
"ID": "9120",
"LastName": "Larkin",
"FirstName": "Shane",
"JerseyNumber": "8",
"Position": "PG"
}
},
{
"position": "Bench7",
"player": {
"ID": "13777",
"LastName": "Theis",
"FirstName": "Daniel",
"JerseyNumber": "27",
"Position": "PF"
}
}
]
},
"actual": null
},
{
"team": {
"ID": "86",
"City": "Cleveland",
"Name": "Cavaliers",
"Abbreviation": "CLE"
},
"expected": {
"starter": [
{
"position": "Bench8",
"player": {
"ID": "13780",
"LastName": "Osman",
"FirstName": "Cedi",
"JerseyNumber": "16",
"Position": "SF"
}
},
{
"position": "Starter1",
"player": {
"ID": "9315",
"LastName": "Wade",
"FirstName": "Dwyane",
"JerseyNumber": "9",
"Position": "SG"
}
},
{
"position": "Starter2",
"player": {
"ID": "9158",
"LastName": "James",
"FirstName": "LeBron",
"JerseyNumber": "23",
"Position": "SF"
}
},
{
"position": "Starter5",
"player": {
"ID": "9161",
"LastName": "Love",
"FirstName": "Kevin",
"JerseyNumber": "0",
"Position": "PF"
}
},
{
"position": "Starter3",
"player": {
"ID": "9142",
"LastName": "Rose",
"FirstName": "Derrick",
"JerseyNumber": "1",
"Position": "PG"
}
},
{
"position": "Starter4",
"player": {
"ID": "9107",
"LastName": "Crowder",
"FirstName": "Jae",
"JerseyNumber": "99",
"Position": "SF"
}
},
{
"position": "Bench1",
"player": {
"ID": "9162",
"LastName": "Shumpert",
"FirstName": "Iman",
"JerseyNumber": "4",
"Position": "SG"
}
},
{
"position": "Bench2",
"player": {
"ID": "9169",
"LastName": "Thompson",
"FirstName": "Tristan",
"JerseyNumber": "13",
"Position": "PF"
}
},
{
"position": "Bench3",
"player": {
"ID": "9167",
"LastName": "Smith",
"FirstName": "J.R.",
"JerseyNumber": "5",
"Position": "SG"
}
},
{
"position": "Bench4",
"player": {
"ID": "9089",
"LastName": "Korver",
"FirstName": "Kyle",
"JerseyNumber": "26",
"Position": "SG"
}
},
{
"position": "Bench5",
"player": {
"ID": "9271",
"LastName": "Green",
"FirstName": "Jeff",
"JerseyNumber": "32",
"Position": "SF"
}
},
{
"position": "Bench6",
"player": {
"ID": "9159",
"LastName": "Frye",
"FirstName": "Channing",
"JerseyNumber": "9",
"Position": "C"
}
},
{
"position": "Bench7",
"player": {
"ID": "9370",
"LastName": "Calderon",
"FirstName": "Jose",
"JerseyNumber": "29",
"Position": "PG"
}
}
]
},
"actual": null
}
]
}
}
答案 0 :(得分:3)
你在timelineup元素上的循环不足以列出你团队中的玩家。你需要一个内循环
foreach (JObject child in obj3["gamestartinglineup"]["teamLineup"])
{
foreach (JObject sub in child["expected"]["starter"])
{
var expected = sub["player"]["FirstName"].ToString() + " " + sub["player"]["LastName"].ToString();
DataRow row = dt.NewRow();
row["Projected Lineup"] = expected;
dt.Rows.Add(row);
}
}
也不要在循环内为gridview分配数据源,但只有当你完成阅读所有内容时
答案 1 :(得分:2)
你在错误的地方使用你的foreach
foreach (JObject child in obj3["gamestartinglineup"]["teamLineup"])
在这部分代码中,循环只包含2个项目!
以及" Marcus Smart"和" Cedi Osman"是每个
的第一个玩家纠正你的foreach循环以解决问题
答案 2 :(得分:2)
你的foreach循环正在循环通过团队阵容为游戏阵容。该阵容中只有两支球队。然后你只抓住每支球队中的第一名球员。相关的代码行是
foreach (JObject child in obj3["gamestartinglineup"]["teamLineup"])
{
sl.expected = child["expected"]["starter"][0]["player"]["FirstName"].ToString() + " " + child["expected"]["starter"][0]["player"]["LastName"].ToString();
foreach正在循环“teamlineup”的孩子们,只有两个孩子,两个团队。
child["extected"]["starter"][0]["player"]
这个名称只为你提供了这两支队伍中的第一个首发球员。
如果你想获得每支球队的所有首发球员,你必须在第一支球队中添加第二个foreach循环,就像这样......
foreach (JObject child in obj3["gamestartinglineup"]["teamLineup"])
{
foreach (JObject player in child["expected"]["starter"])
{