JavaEar 专注于收集分享传播有价值的技术资料

C# Cannot access child value on Newtonsoft.Json.Linq.JProperty

I was hoping you guys may be able to assist in helping me out. I would like to pull every ID within the JSON file however I am getting the following exception error:

Cannot access child value on Newtonsoft.Json.Linq.JProperty.

    private void button1_Click(object sender, EventArgs e)
    {

        string address = "https://api.mysportsfeeds.com/v1.1/pull/nba/2016-2017-regular/cumulative_player_stats.json?team=85";
        var w = new WebClient();
        w.UseDefaultCredentials = true;
        w.Credentials = new NetworkCredential("****", "****");
        var result = w.DownloadString(address);

        var obj = JObject.Parse(result);
        var play = "";


        foreach (JProperty child in obj["cumulativeplayerstats"])
        {
            play = child["player"]["ID"].ToString();

            string latlong = Convert.ToString(play);
            Console.WriteLine("player=" + Convert.ToString(play));
            Console.ReadKey();
        }
    }

Here is a snippet of what the json looks like:

{
  "cumulativeplayerstats": {
    "lastUpdatedOn": "2017-09-11 4:45:30 PM",
    "playerstatsentry": [
      {
        "player": {
          "ID": "10138",
          "LastName": "Abrines",
          "FirstName": "Alex",
          "JerseyNumber": "8",
          "Position": "F"
        },
        "team": {
          "ID": "96",
          "City": "Oklahoma City",
          "Name": "Thunder",
          "Abbreviation": "OKL"
        },
        "stats": {
          "GamesPlayed": {
            "@abbreviation": "GP",
            "#text": "68"
          },
          "Fg2PtAtt": {
            "@category": "Field Goals",
            "@abbreviation": "2PA",
            "#text": "94"
          },
          "Fg2PtMade": {
            "@category": "Field Goals",
            "@abbreviation": "2PM",
            "#text": "40"
          },
          "Fg3PtAtt": {
            "@category": "Field Goals",
            "@abbreviation": "3PA",
            "#text": "247"
          },
          "Fg3PtMade": {
            "@category": "Field Goals",
            "@abbreviation": "3PM",
            "#text": "94"
          },
          "FtAtt": {
            "@category": "Free Throws",
            "@abbreviation": "FTA",
            "#text": "49"
          },
          "FtMade": {
            "@category": "Free Throws",
            "@abbreviation": "FTM",
            "#text": "44"
          }
        }
      },

I tried digging around on here but I cant seem to find a solution that works. If you guys can lend some of your sage wisdom, I would greatly appreciate it!

Thanks Folks!

2个回答

    最佳答案
  1. You can modify your foreach to iterate through the playerstatsentry array:

    foreach (JObject child in obj["cumulativeplayerstats"]["playerstatsentry"].OfType<JObject>())
    

    Or without the OfType<T>:

    foreach (var child in obj["cumulativeplayerstats"]["playerstatsentry"])
    
  2. 参考答案2
  3. Taking your JSON as an example, if you are trying to locate all the properties named "ID", provided they are at same level of the hierarchy, you can utilize the "SelectTokens" method using path and wildcards to reach to your property level. This will probably save a few lines of code and is a better way to locate.

    .net Fiddle for this solution - https://dotnetfiddle.net/fQ9xeH

    foreach (var tokn in 
            obj.SelectTokens("cumulativeplayerstats.playerstatsentry[*].*.ID"))
            {               
                Console.WriteLine(tokn);
            }