解析json并获得一个nullexception,无论我做了多少次nullcheck

时间:2017-04-24 19:15:05

标签: c# json linq json.net

我试图解析有时会返回null包的JSON流:

{"package":null}

我已经尝试了几乎所有我能找到的东西来检查一个空包但我仍然得到了应该有效的jObjects的例外。 这是我的代码

string json = APIRequests.RestClient.makeRequest(uri);
        JObject jObject = JObject.Parse(json);
        JToken token = jObject["killID"];
        try
        {
            if (JsonExtensions.IsNullOrEmpty(token))
            {
                Kill kill = new Kill(
                    true,
                    (int)jObject.SelectToken("package.killmail.killID"),
                    (int)jObject.SelectToken("package.killmail.victim.alliance.id_str"),
                    (string)jObject.SelectToken("package.killmail.victim.alliance.name")
                    );
                return kill;
            }
            else
            {
                Kill nullKill = new Kill(false);
                return nullKill;
            }
        }
        catch(Exception ex)
        {
            Kill nullKill = new Kill(false);
            Console.WriteLine(ex + jObject.ToString());
            return nullKill;

        }

JsonExtensions类:

 public static class JsonExtensions
{
    public static bool IsNullOrEmpty(this JToken token)
    {
        return (token == null) ||
               (token.Type == JTokenType.Array && !token.HasValues) ||
               (token.Type == JTokenType.Object && !token.HasValues) ||
               (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
               (token.Type == JTokenType.Null);
    }
}

当我尝试实例化“杀戮”时会出现例外情况。宾语。 这就是' Kill'看起来:

 public class Kill
{
    public bool valid;
    public int killid;
    public int allianceid;
    public string alliancename;
    public Kill(bool _valid, int _killid = -1, int _allianceid = -1, string _alliancename = "null")
    {
        valid = _valid;
        killid = _killid;
        allianceid = _allianceid;
        alliancename = _alliancename;
    }
}

这是错误:

> System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at Newtonsoft.Json.Linq.JToken.EnsureValue(JToken value)
   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
   at ThiccBott.ObjectParser.ParseKill(String uri) in C:\Users\user\Documents\c#\thiccbott\Utils\ThiccBott.ZkillParser.cs:line 21{
  "package": {
    "killID": 61807875,
    "killmail": {
      "solarSystem": {
        "id_str": "30045306",
        "href": "https://crest-tq.eveonline.com/solarsystems/30045306/",
        "id": 30045306,
        "name": "Hykanima"
      },
      "killID": 61807875,
      "killTime": "2017.04.24 18:39:42",............................

Docs on the very simple API I'm using

1 个答案:

答案 0 :(得分:1)

您需要检查token null或为空。您的代码缺少 !

此外,对于有效响应,您需要将token = jObject["killID"];替换为token = jObject.SelectToken("package.killID");

JObject jObject = JObject.Parse(json);
//JToken token = jObject["killID"];
var token = jObject.SelectToken("package.killID");

if (!JsonExtensions.IsNullOrEmpty(token))
{
    Kill kill = new Kill(
        true,
        (int)jObject.SelectToken("package.killmail.killID"),
        (int)jObject.SelectToken("package.killmail.victim.alliance.id_str"),
        (string)jObject.SelectToken("package.killmail.victim.alliance.name")
        );
    return kill;
}
else
{
    Kill nullKill = new Kill(false);
    return nullKill;
}

示例fiddle