简体   繁体   English

嵌套值为null时反序列化的Restsharp错误

[英]Restsharp error on deserialzation when nested value is null

I'm running into an error when a nested value is null. 当嵌套值为null时,我遇到了错误。 If the value is not null everything works as expected. 如果该值不为null,则一切正常。 This does not happen if the value is not nested. 如果未嵌套该值,则不会发生这种情况。

The error is: 错误是:

InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'. InvalidCastException:无法将类型为“ System.String”的对象转换为类型为“ System.Collections.Generic.IDictionary`2 [System.String,System.Object]”。

The error happens when I'm checking response.ErrorException != null on the List Contract 当我检查列表合同上的response.ErrorException!= null时发生错误

Json returned: Contract administrator is nested and blank error: ends is not nested blank and no error: Json返回:合同管理员嵌套且为空错误:两端未嵌套为空白且无错误:

 "result": [
    {
      "sys_id": "06dc3133db1747808c47499e0b96192e",
      "number": "CNTR001234",
      "short_description": "Contract 123",
      "u_internal_contact": {
        "link": "https://website",
        "value": "5b4080490a0a3c9e016cb2a9f4eb57b1"
      },
      "vendor": {
        "link": "https://website",
        "value": "b7e7c073c0a801690143e7b7d29eb408"
      },
      "ends": "",
      "payment_amount": "60000",
      "u_status": "Active",
      "starts": "2018-01-01",
      "contract_administrator": ""
    }
  ]
}

Code

public class Results
{
    public List<Contract> items { get; set; }
}
public class Contract
{
    public string sys_id { get; set; }
    public string number { get; set; }
    public string short_description { get; set; }
    public string ends { get; set; }
    public string payment_amount { get; set; }
    public string u_status { get; set; }
    public string starts { get; set; }
    public Vendor vendor { get; set; }
    public ContractAdmin contract_administrator { get; set; }
    public InternalContact u_internal_contact { get; set; }

}

public class Vendor
{
    public string link { get; set; }
    public string value { get; set; }
}

public class ContractAdmin
{
    public string link { get; set; }
    public string value { get; set; }
}

public class InternalContact
{
    public string link { get; set; }
    public string value { get; set; }
}

public class refResults
{
    public List<refName> itemName { get; set; }
}
public class refName
{
    public string name { get; set; }
}

class ImportContracts
{

    public static void ProcessImport()
    {

        RestClient contractsRequest = new RestClient(Properties.Settings.Default.RestURL);
        contractsRequest.Authenticator = new HttpBasicAuthenticator(Properties.Settings.Default.userName, Properties.Settings.Default.password);
        contractsRequest.AddHandler("application/json", new RestSharp.Deserializers.JsonDeserializer());

        RestRequest request = new RestRequest();
        request.RootElement = "result";
        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

        IRestResponse<List<Contract>> response = contractsRequest.Execute<List<Contract>>(request);
        Console.WriteLine(response.Content);
        if (response.ErrorException != null)
        {
            const string message = "Error retrieving response.  Check inner details for more info.";
            var ex = new ApplicationException(message, response.ErrorException);
            throw ex;
        }
        foreach (Contract contract in response.Data)
        {
            //Console.WriteLine(contract.sys_id);
            string strVendor = GetName(contract.vendor.link.ToString());
            string strInternalContact = GetName(contract.u_internal_contact.link.ToString());
            string strContractAdmin = GetName(contract.contract_administrator.ToString());
        }


    }

    static public string GetName (string link)
    {
        RestClient nameRequest = new RestClient(link);
        nameRequest.Authenticator = new HttpBasicAuthenticator(Properties.Settings.Default.userName, Properties.Settings.Default.password);
        nameRequest.AddHandler("application/json", new RestSharp.Deserializers.JsonDeserializer());

        RestRequest requestedName = new RestRequest();
        requestedName.RootElement = "result";
        requestedName.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

        IRestResponse<List<refName>> response = nameRequest.Execute<List<refName>>(requestedName);
        if (response.ErrorException != null)
        {
            const string message = "Error retrieving response.  Check inner details for more info.";
            var ex = new ApplicationException(message, response.ErrorException);
            throw ex;
        }


        foreach (refName refname in response.Data)
        {

            return refname.name;
        }

        return "name not found";
    }




}

Any help would be appreciated! 任何帮助,将不胜感激!

Looking at your JSON, "contract_administrator" is not null , it's an empty string. 查看您的JSON, "contract_administrator"不是null ,而是一个空字符串。 Your contract requires a ContractAdmin object, so what it's likely doing is attempting to cast an empty string to a ContractAdmin . 您的合同需要一个ContractAdmin对象,因此它可能正在尝试将空字符串转换为ContractAdmin

If you change "contract_administrator" to be null instead of an empty string, I'm willing to bet that it will parse correctly. 如果将"contract_administrator"更改为null而不是空字符串,我敢打赌它会正确解析。

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

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