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

时间:2018-10-24 14:30:13

标签: c# json nested deserialization restsharp

当嵌套值为null时,我遇到了错误。如果该值不为null,则一切正常。如果未嵌套该值,则不会发生这种情况。

错误是:

  

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

当我检查response.ErrorException!=空值时,发生错误

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": ""
    }
  ]
}

代码

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";
    }




}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

看您的JSON,"contract_administrator"不是null,它是一个空字符串。您的合同需要一个ContractAdmin对象,因此它可能正在尝试将空字符串强制转换为ContractAdmin

如果您将"contract_administrator"更改为null而不是空字符串,我敢打赌它将正确解析。