RestSharp返回具有空值的对象

时间:2019-10-21 19:03:12

标签: restsharp json-deserialization

我正在使用RestSharp 106.6.10。我希望响应返回一个具有四个字符串属性的简单对象。我已经用正确的属性类型,拼写和大小写在C#中定义了对象的类。

我正在发布请求并获得没有错误的回复。响应的Content似乎是纯JSON,带有一个Data对象,该对象包含我的“ TransactionAcknowledgement”对象的良好值。但这是我的问题:RestSharp返回的Data对象的反序列化属性具有空值。

我尝试了SO和其他站点的一些建议:在类属性上添加“ DeserializeAs”提示;使用OnBeforeDeserialization设置ContentType =“ application / json”;还有其他一些其他建议。似乎没有任何帮助。

这是类的定义:

public class TransactionAcknowledgement
{
    [DeserializeAs(Name = "BusinessEntityID")] // These hints don't seem to help.
    public string BusinessEntityID { get; set; }

    [DeserializeAs(Name = "BusinessEntityMedicaidIdentifier")]
    public string BusinessEntityMedicaidIdentifier { get; set; }/

    [DeserializeAs(Name = "TransactionID")]
    public string TransactionID { get; set; }

    [DeserializeAs(Name = "Reason")]
    public string Reason { get; set; }
}

还有一个C#代码段:

RestClient restClient;
RestRequest restRequest;
IRestResponse<TransactionAcknowledgement> restResponse;

restClient = new RestClient(MyBaseUrl);
restClient.Authenticator = new HttpBasicAuthenticator(evvCompanyAggregator.EffectiveUserID, evvCompanyAggregator.EffectiveUserPassword);

restRequest = new RestRequest("MyResource", Method.POST, DataFormat.Json);
restRequest.AddJsonBody(myData);
restRequest.OnBeforeDeserialization = r => { r.ContentType = "application/json"; }; // This doesn't seem to help.
restResponse = restClient.Execute<TransactionAcknowledgement>(restRequest);

restResponse.Content看起来不错,包括原始restResponse.Content字符串内“ Data”对象中的好值。 restResponse.Content.Data对象也是正确的类TransactionAcknowledgement。 但是,restResponse.Content.Data中的属性值都为空。

这是原始的restResponse.Content字符串:

"{\n  \"id\": \"1de51d1a-8086-4ba7-8aa6-2d1431986a99\",\n  \"status\": null,\n  \"token\": null,\n  \"messageSummary\": \"Transaction Received.\",\n  \"messageDetail\": null,\n  \"errorMessage\": null,\n  \"failedCount\": 0,\n  \"succeededCount\": 0,\n  \"cached\": false,\n  \"cachedDate\": null,\n  \"totalRows\": 0,\n  \"page\": 0,\n  \"pageSize\": 0,\n  \"orderByColumn\": null,\n  \"orderByDirection\": null,\n  \"data\": {\n    \"BusinessEntityID\": \"200248\",\n    \"BusinessEntityMedicaidIdentifier\": \"8471209\",\n    \"TransactionID\": \"1de51d1a-8086-4ba7-8aa6-2d1431986a99\",\n    \"Reason\": \"Transaction Received.\"\n  }\n}"

这是相同的restResponse.Content的格式经过精心设计以提高可读性:

{
 "id": "bf0606a3-f21d-4d51-980c-bee407adc561",
 "status": null,
 "token": null,
 "messageSummary": "Transaction Received.",
 "messageDetail": null,
 "errorMessage": null,
 "failedCount": 0,
 "succeededCount": 0,
 "cached": false,
 "cachedDate": null,
 "totalRows": 0,
 "page": 0,
 "pageSize": 0,
 "orderByColumn": null,
 "orderByDirection": null,
 "data": {
   "BusinessEntityID": "200248",
   "BusinessEntityMedicaidIdentifier": "8471209",
   "TransactionID": "bf0606a3-f21d-4d51-980c-bee407adc561",
   "Reason": "Transaction Received."
 }
}

这是调试器中的对象,显示空属性值: Object with null values

我花了几天时间。我确信这对大多数人都有效,所以我可能只是缺少一些简单的东西。有什么建议吗?

更新: 为了证明响应中包含的数据是可以的并且与我的类定义匹配,我直接反序列化了响应内容。我创建了一个新的包装器类:

public class HttpRestRootObject
{
    public TransactionAcknowledgement data { get; set; }
}

然后,我将没有RestSharp的restResponse.Content字符串反序列化:

HttpRestRootObject testRoot = javaScriptSerializer.Deserialize<HttpRestRootObject>(restResponse.Content);
TransactionAcknowledgement transactionAcknowledgement = testRoot.data;

因此,所有内容均正常运行, RestSharp的反序列化。我以某种方式使用RestSharp是否出错?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我有完全相同的问题。我发现的唯一解决方法是恢复到 RestSharp 106.10.1。所有 106.11.x 都有同样的问题。也可以声明您自己的自定义序列化程序,这可能只是 NewtonSoft,但是,根据您序列化/反序列化内容的复杂性,这可能比它的价值更令人头疼。