简体   繁体   English

调用JObject.Parse()时处理null

[英]Dealing with null when calling JObject.Parse()

In the below code, I'm getting an exception due the value with in JObject.Parse() resulting in a null, which of course can't be parsed. 在下面的代码中,由于JObject.Parse()中的值导致空值,因此我得到了异常,这当然是无法解析的。

JObject result = JObject.Parse(responseData["Result"].ToString());

To try and work around this I tried the following so that a null check was done before calling parse. 为了解决此问题,我尝试了以下操作,以便在调用parse之前进行空检查。

var resultString = string.IsNullOrEmpty(responseData["Result"].ToString()) ? "" : responseData["Result"].ToString();
JObject result = JObject.Parse(resultString);

This still causes an exception though, I feel like I'm just overlooking something very small here. 不过,这仍然会导致异常,我觉得我只是在这里忽略了一些很小的东西。 What can I do to address a possible null value here. 我该怎么办才能解决此处可能为null的值。

This has nothing to do with JObject.Parse - I suspect that will handle a null by just returning a null, or empty, JObject. 这与JObject.Parse -我怀疑仅通过返回空(或空)JObject即可处理空值。

Most likely is that you;re missing that responseData["Result"] is null, and you're trying to call ToString() on it. 最有可能是您;缺少responseData["Result"]为null,并且您试图在其上调用ToString() This will cause a NullReferenceException 这将导致NullReferenceException

Fix it by checking for null: 通过检查null来修复它:

var resultString = (responseData["Result"] == null || string.IsNullOrEmpty(responseData["Result"].ToString())) 
    ? "" 
    : responseData["Result"].ToString();

JObject result = JObject.Parse(responseData["Result"].ToString()); could be throwing an exception for several reasons: 可能由于某些原因引发异常:

  • If responseData itself is null, then trying to access responseData["Result"] will throw an exception. 如果responseData本身为null,则尝试访问responseData["Result"]将引发异常。
  • If responseData["Result"] is null (eg if there is no key called "Result" present in responseData ) then trying to access responseData["Result"].ToString() will throw an exception. 如果responseData["Result"]为null(例如,如果responseData没有名为“ Result”的键),则尝试访问responseData["Result"].ToString()将引发异常。
  • If responseData["Result"].ToString() is null (or empty), then JObject.Parse() will throw because null is not a valid JSON string. 如果responseData["Result"].ToString()为null(或为空),则将抛出JObject.Parse()因为null不是有效的JSON字符串。

Merely checking string.IsNullOrEmpty(responseData["Result"].ToString()) is not good enough to fix it because it only addresses the last of these three cases. 仅检查string.IsNullOrEmpty(responseData["Result"].ToString())不足以对其进行修复,因为它仅解决了这三种情况中的最后一种情况。 Most likely the exception is being thrown before the ToString() call, and you are only checking whether the result of ToString() is null. 最有可能在ToString()调用之前引发了异常,并且您仅在检查ToString()结果是否为null。 By that point it is too late. 到那时,为时已晚。

When you run into situations like this where you have a complex line of code and it is throwing an exception somewhere within it, it helps to break it down and do null checks at each step. 当您遇到这样的情况时,即您的代码行很复杂,并且在其中的某个地方引发了异常,这有助于将其分解并在每个步骤中执行空检查。 That way you can easily step through with a debugger and see exactly what is going on. 这样一来,您就可以轻松地通过调试器进行调试,并准确了解正在发生的情况。

JObject result = null;
if (responseData != null)
{
    object obj = responseData["Result"];
    if (obj != null)
    {
        string str = obj.ToString();
        if (!string.IsNullOrEmpty(str))
        {
            result = JObject.Parse(str);
        }
    }
}

Having said all that, I want to point out that you seem to be making your code more complex than it needs to be in the first place. 说了这么多,我想指出的是,您似乎使代码变得比最初需要的更加复杂。 You didn't say in your question what responseData is, but based on how you're using it I'm guessing it is a JObject . 您没有在问题中说什么是responseData ,但是根据您的使用方式,我猜测它是一个JObject If that is true, you shouldn't need to convert responseData["result"] to string only to then re-parse it back into a JObject . 如果是这样,则不需要只将responseData["result"]转换为字符串,然后再将其重新解析为JObject Instead all you need to do is cast it: 相反,您所需要做的就是强制转换它:

JObject result = responseData != null ? responseData["Result"] as JObject : null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM