当反序列化JSON响应时,RestSharp客户端将返回所有属性为null [英] RestSharp client returns all properties as null when deserializing JSON response
问题描述
这里是JSON响应:
{
Result:
{
Location:
{
BusinessUnit :BTA,
BusinessUnitName:CASINO,
LocationId:4070,
LocationCode:ZBTA,
LocationName:赌场名称
}
}
}
我的测试代码
[TestMethod]
public void TestLocationsGetById()
{
// given
var request = new RestRequest();
request.Resource = serviceEndpoint +/ {singleItemTestId};
request.Method = Method.GET;
request.AddHeader(accept,Configuration.JSONContentType);
request.RootElement =Location;
request.AddParameter(singleItemTestId,singleItemTestId,ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
//当
时位置位置= api.Execute< Location>(request);
// then
Assert.IsNotNull(location.LocationId); // failed - all properties are returned null
}
我的API代码
public T Execute< T>(RestRequest request)其中T:new()
{
var client = new RestClient();
client.BaseUrl = Configuration.ESBRestBaseURL;
//request.OnBeforeDeserialization = resp => {resp.ContentLength = 761; };
var response = client.Execute< T>(request);
return response.Data;
}
最后,这里是我的POCO
public class Location
{
public string BusinessUnit {get;组; }
public string BusinessUnitName {get;组; }
public string LocationId {get;组; }
public string LocationCode {get;组; }
public string LocationName {get;组; }
}
此外,响应中的ErrorException和ErrorResponse属性为NULL。
这看起来是一个非常简单的例子,但我一直在运行在全天!感谢。
响应中的 Content-Type
是什么?如果不是像application / json这样的标准内容类型,则RestSharp不会理解哪个解串器使用。如果事实上RestSharp不能理解一个内容类型(您可以通过检查请求中发送的 Accept
来验证),那么您可以通过执行以下操作来解决这个问题: / p>
client.AddHandler(my_custom_type,new JsonDeserializer());
编辑:
好的,对不起,再次查看JSON,你需要像:
public class LocationResponse
public LocationResult Result {get;组; }
}
public class LocationResult {
public Location Location {get;组; }
}
然后执行:
$ b b
client.Execute< LocationResponse>(request);
I'm trying to do a very simple example of using RestSharp's Execute method of querying a rest endpoint and serializing to a POCO. However, everything I try results in a response.Data object that has all properties with a NULL value.
Here is the JSON response:
{
"Result":
{
"Location":
{
"BusinessUnit": "BTA",
"BusinessUnitName": "CASINO",
"LocationId": "4070",
"LocationCode": "ZBTA",
"LocationName": "Name of Casino"
}
}
}
Here is my test code
[TestMethod]
public void TestLocationsGetById()
{
//given
var request = new RestRequest();
request.Resource = serviceEndpoint + "/{singleItemTestId}";
request.Method = Method.GET;
request.AddHeader("accept", Configuration.JSONContentType);
request.RootElement = "Location";
request.AddParameter("singleItemTestId", singleItemTestId, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
//when
Location location = api.Execute<Location>(request);
//then
Assert.IsNotNull(location.LocationId); //fails - all properties are returned null
}
And here is my API code
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = Configuration.ESBRestBaseURL;
//request.OnBeforeDeserialization = resp => { resp.ContentLength = 761; };
var response = client.Execute<T>(request);
return response.Data;
}
And finally, here is my POCO
public class Location
{
public string BusinessUnit { get; set; }
public string BusinessUnitName { get; set; }
public string LocationId { get; set; }
public string LocationCode { get; set; }
public string LocationName { get; set; }
}
Additionally, the ErrorException and ErrorResponse properties on the response are NULL.
This seems like a very simple case, but I've been running around in circles all day! Thanks.
What is the Content-Type
in the response? If not a standard content type like "application/json", etc. then RestSharp won't understand which deserializer to use. If it is in fact a content type not "understood" by RestSharp (you can verify by inspecting the Accept
sent in the request), then you can solve this by doing:
client.AddHandler("my_custom_type", new JsonDeserializer());
EDIT:
Ok, sorry, looking at the JSON again, you need something like:
public class LocationResponse
public LocationResult Result { get; set; }
}
public class LocationResult {
public Location Location { get; set; }
}
And then do:
client.Execute<LocationResponse>(request);
这篇关于当反序列化JSON响应时,RestSharp客户端将返回所有属性为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!