C# foreach loop is only displaying 2 results within the entire json file

Michael Lewis

I need every players first and last name in JSON file to populate within in a datagridview. Everything is working fine except I am only getting 2 players in the dgv. Both players are after the expected portion of the json, so the only 2 players that are showing for me are "Marcus Smart" and "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
          }
        ]
      }
    }
Steve

Your loop on the timelineup element is not enough to list of the players in your team. You need an inner loop

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);
    }
}

Also do not assign the datasource for the gridview inside the loop but only when you finish to read everything

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQLite: return only top 2 results within each group

分類Dev

increment the values of foreach within ifelse loop in C#

分類Dev

C# Make async http requests within a foreach loop

分類Dev

Add links within a foreach loop

分類Dev

Getting duplicated results in foreach loop

分類Dev

Why is my foreach loop not matching the keys in users.json file

分類Dev

PHP file structure array to json string only displaying first file properly

分類Dev

Changing a foreach loop to an if statement for reading a text file C#

分類Dev

Foreach Loop only loops once

分類Dev

Get a specific element within forEach loop

分類Dev

Loop through read only .zip data archive to extract line from .kml file within

分類Dev

For loop producing unexpected results in C

分類Dev

Refer to a value just set in array within foreach loop

分類Dev

concatinating two array elements with a string within a foreach loop

分類Dev

Populating a list with results from queue within a while loop in scala

分類Dev

How to only deserialize part of a Json file into a class c#

分類Dev

How to limit foreach loop to 3 results as icons in PHP?

分類Dev

editing the ~/.zshrc file—how to add coloring to only directories and not the entire prompt?

分類Dev

Displaying inline within a list

分類Dev

PHP 2 Arrays in Foreach-loop?

分類Dev

Display 2 Array values in foreach() loop - PHP

分類Dev

scraper only returning results for first 2 inputs

分類Dev

Foreach loop stores only first index of multidimensional array | Laravel

分類Dev

PHP While loop only works for first two of three results?

分類Dev

Edit a File in Python and write the results on a new file but only in specific areas

分類Dev

Is there a method for getting the results from the JSON-file?

分類Dev

Reading and Displaying content of the Text File via C coding

分類Dev

Reading and Displaying content of the Text File via C coding

分類Dev

C# - Displaying a message before new file is opened

Related 関連記事

  1. 1

    SQLite: return only top 2 results within each group

  2. 2

    increment the values of foreach within ifelse loop in C#

  3. 3

    C# Make async http requests within a foreach loop

  4. 4

    Add links within a foreach loop

  5. 5

    Getting duplicated results in foreach loop

  6. 6

    Why is my foreach loop not matching the keys in users.json file

  7. 7

    PHP file structure array to json string only displaying first file properly

  8. 8

    Changing a foreach loop to an if statement for reading a text file C#

  9. 9

    Foreach Loop only loops once

  10. 10

    Get a specific element within forEach loop

  11. 11

    Loop through read only .zip data archive to extract line from .kml file within

  12. 12

    For loop producing unexpected results in C

  13. 13

    Refer to a value just set in array within foreach loop

  14. 14

    concatinating two array elements with a string within a foreach loop

  15. 15

    Populating a list with results from queue within a while loop in scala

  16. 16

    How to only deserialize part of a Json file into a class c#

  17. 17

    How to limit foreach loop to 3 results as icons in PHP?

  18. 18

    editing the ~/.zshrc file—how to add coloring to only directories and not the entire prompt?

  19. 19

    Displaying inline within a list

  20. 20

    PHP 2 Arrays in Foreach-loop?

  21. 21

    Display 2 Array values in foreach() loop - PHP

  22. 22

    scraper only returning results for first 2 inputs

  23. 23

    Foreach loop stores only first index of multidimensional array | Laravel

  24. 24

    PHP While loop only works for first two of three results?

  25. 25

    Edit a File in Python and write the results on a new file but only in specific areas

  26. 26

    Is there a method for getting the results from the JSON-file?

  27. 27

    Reading and Displaying content of the Text File via C coding

  28. 28

    Reading and Displaying content of the Text File via C coding

  29. 29

    C# - Displaying a message before new file is opened

ホットタグ

アーカイブ