C#如何处理NULL json值?获取和InvalidOperationException:无法访问>上的子值Newtonsoft.Json.Linq.JValue

时间:2017-10-24 22:21:31

标签: c# json

我在处理json文件中的空值时遇到问题。正如你在下面的照片中看到的那样,空值在播放器下:

enter image description here

不幸的是,这导致了以下InvalidOperationException:

  

System.InvalidOperationException:无法访问子值   Newtonsoft.Json.Linq.JValue。        at Newtonsoft.Json.Linq.JToken.get_Item(Object key)        at WindowsFormsApplication2.Starting_Lineups.StartingLineups()在C:\ Users \ Administrator \ documents \ visual studio中   2015年\项目\ WindowsFormsApplication2 \ WindowsFormsApplication2 \启动   Lineups.cs:第97行

我尝试了以下How to ignore a property in class if null, using json.net,但未成功。

JSON:

{"gamestartinglineup":{"lastUpdatedOn":"2017-10-24 11:41:12 AM","game":{"id":"42120","date":"2017-10-24","time":"7:30PM","awayTeam":{"ID":"83","City":"New York","Name":"Knicks","Abbreviation":"NYK"},"homeTeam":{"ID":"82","City":"Boston","Name":"Celtics","Abbreviation":"BOS"},"location":"TD Garden"},"teamLineup":[{"team":{"ID":"83","City":"New York","Name":"Knicks","Abbreviation":"NYK"},"expected":{"starter":[{"position":"Bench8","player":null},{"position":"Starter1","player":{"ID":"9087","LastName":"Hardaway Jr.","FirstName":"Tim","JerseyNumber":"3","Position":"SG"}},{"position":"Starter2","player":{"ID":"9373","LastName":"Porzingis","FirstName":"Kristaps","JerseyNumber":"6","Position":"PF"}},{"position":"Starter5","player":{"ID":"9518","LastName":"Sessions","FirstName":"Ramon","JerseyNumber":"1","Position":"PG"}},{"position":"Starter3","player":{"ID":"9396","LastName":"Kanter","FirstName":"Enes","JerseyNumber":"0","Position":"C"}},{"position":"Starter4","player":{"ID":"9127","LastName":"Lee","FirstName":"Courtney","JerseyNumber":"1","Position":"SG"}},{"position":"Bench1","player":{"ID":"9148","LastName":"McDermott","FirstName":"Doug","JerseyNumber":"20","Position":"SF"}},{"position":"Bench2","player":{"ID":"9376","LastName":"O'Quinn","FirstName":"Kyle","JerseyNumber":"9","Position":"PF"}},{"position":"Bench3","player":{"ID":"9245","LastName":"Beasley","FirstName":"Michael","JerseyNumber":"9","Position":"F"}},{"position":"Bench4","player":{"ID":"10102","LastName":"Baker","FirstName":"Ron","JerseyNumber":"31","Position":"G"}},{"position":"Bench5","player":null},{"position":"Bench6","player":null},{"position":"Bench7","player":null}]},"actual":null},{"team":{"ID":"82","City":"Boston","Name":"Celtics","Abbreviation":"BOS"},"expected":{"starter":[{"position":"Bench8","player":null},{"position":"Starter1","player":{"ID":"13730","LastName":"Tatum","FirstName":"Jayson","JerseyNumber":"0","Position":"F"}},{"position":"Starter2","player":{"ID":"9082","LastName":"Horford","FirstName":"Al","JerseyNumber":"15","Position":"C"}},{"position":"Starter5","player":{"ID":"9157","LastName":"Irving","FirstName":"Kyrie","JerseyNumber":"11","Position":"PG"}},{"position":"Starter3","player":{"ID":"9211","LastName":"Baynes","FirstName":"Aron","JerseyNumber":"12","Position":"C"}},{"position":"Starter4","player":{"ID":"10090","LastName":"Brown","FirstName":"Jaylen","JerseyNumber":"7","Position":"SF"}},{"position":"Bench1","player":{"ID":"13777","LastName":"Theis","FirstName":"Daniel","JerseyNumber":"27","Position":"PF"}},{"position":"Bench2","player":{"ID":"9105","LastName":"Rozier","FirstName":"Terry","JerseyNumber":"12","Position":"PG"}},{"position":"Bench3","player":{"ID":"13770","LastName":"Nader","FirstName":"Abdel","JerseyNumber":"28","Position":"SF"}},{"position":"Bench4","player":{"ID":"9120","LastName":"Larkin","FirstName":"Shane","JerseyNumber":"8","Position":"PG"}},{"position":"Bench5","player":{"ID":"13778","LastName":"Yabusele","FirstName":"Guerschon","JerseyNumber":"30","Position":"PF"}},{"position":"Bench6","player":{"ID":"13771","LastName":"Bird","FirstName":"Jabari","JerseyNumber":"26","Position":"SG"}},{"position":"Bench7","player":null}]},"actual":null}]}}

代码:

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


        StartingLineup.startinglineup sl = new StartingLineup.startinglineup();

        try
       {

            foreach (JToken child in obj3 ["gamestartinglineup"]["teamLineup"])
            {


                    foreach (JToken sub in child["expected"]["starter"])
                    {

                    JsonConvert.SerializeObject(sub,
                        Newtonsoft.Json.Formatting.None,
                        new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        });

                    sl.player = sub["player"]["FirstName"].ToString() + " " + sub["player"]["LastName"].ToString();
                        sl.team = child["team"]["City"].ToString() + " " + child["team"]["Name"].ToString();
                        sl.position = sub["position"].ToString();
                        sl.playerpositon = sub["player"]["Position"].ToString();




                        DataRow row = dt.NewRow();
                        row["Team"] = sl.team;
                        row["Player"] = sl.player;
                        row["Position"] = sl.playerpositon;





                        if (sl.position.Contains("Starter"))
                        {
                            row["Role"] = sl.position;
                        }
                        else
                        {
                            row["Role"] = "NOPE";
                        }



                        dt.Rows.Add(row);
                    }
                    dataGridView1.DataSource = dt;

                    DataRow[] drr = dt.Select("Role='NOPE'");
                    for (int i = 0; i < drr.Length; i++)
                        drr[i].Delete();
                    dt.AcceptChanges();




                }



        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());

        }

    }

1 个答案:

答案 0 :(得分:1)

在检查可空值(Value<T>(Object key))时考虑这一点:

var testObj = new Newtonsoft.Json.Linq.JObject();

if (testObj.TryGetValue("player", out Newtonsoft.Json.Linq.JToken token)) {
 string yourString = token.Value<string>("FirstName"); // can be null
 int? yourNullableInt = token.Value<int?>("AnyNumber"); // can be null
 int yourInt = token.Value<int?>("AnyNumber") ?? 0; // can be 0 if null
 int yourInt = token.SelectToken("firstLayer")?.SelectToken("secondLayer")?.Value<int?>("anyNumber") ?? 0; // return value by path
}

SelectToken也适用于Newtonsoft.Json.Linq.JObject