【发布时间】:2019-10-21 19:03:12
【问题描述】:
我正在使用 RestSharp 106.6.10。我希望响应返回一个具有四个字符串属性的简单对象。我已经用正确的属性类型、拼写和大小写在 C# 中定义了对象的类。
我正在发布请求并得到没有错误的响应。响应的内容似乎是干净的 JSON,带有一个包含我的“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#代码sn-p:
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."
}
}
我在这上面花了几天时间。我相信这对大多数人都有效,所以我可能只是缺少一些简单的东西。有什么建议吗?
更新: 为了证明响应进来的数据没问题并且和我的类定义相匹配,我直接反序列化了响应内容。我创建了一个新的包装类:
public class HttpRestRootObject
{
public TransactionAcknowledgement data { get; set; }
}
然后,我反序列化了没有 RestSharp 的 restResponse.Content 字符串:
HttpRestRootObject testRoot = javaScriptSerializer.Deserialize<HttpRestRootObject>(restResponse.Content);
TransactionAcknowledgement transactionAcknowledgement = testRoot.data;
所以,一切正常除了 RestSharp 的反序列化。我是否以某种方式错误地使用了 RestSharp?有什么建议吗?
【问题讨论】:
标签: restsharp json-deserialization