相关疑难解决方法(0)

在反序列化JSON响应时,RestSharp客户端将所有属性返回为null

我正在尝试使用RestSharp的Execute方法查询休息端点并序列化为POCO的一个非常简单的示例.但是,我尝试的所有内容都会产生一个response.Data对象,该对象具有NULL值的所有属性.

这是JSON响应:

{
   "Result":
   {
       "Location":
       {
           "BusinessUnit": "BTA",
           "BusinessUnitName": "CASINO",
           "LocationId": "4070",
           "LocationCode": "ZBTA",
           "LocationName": "Name of Casino"
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码

 [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

    }
Run Code Online (Sandbox Code Playgroud)

这是我的API代码

 public T Execute<T>(RestRequest request) where T : new()
    {
        var client …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp c#-4.0

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

c#-4.0 ×1

json ×1

restsharp ×1