【问题标题】:RestSharp client returns all properties as null when deserializing JSON response反序列化 JSON 响应时,RestSharp 客户端将所有属性返回为 null
【发布时间】:2012-06-18 16:55:57
【问题描述】:

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

这是 JSON 响应:

{
   "Result":
   {
       "Location":
       {
           "BusinessUnit": "BTA",
           "BusinessUnitName": "CASINO",
           "LocationId": "4070",
           "LocationCode": "ZBTA",
           "LocationName": "Name of Casino"
       }
   }
}

这是我的测试代码

 [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

    }

这是我的 API 代码

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

最后,这是我的 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; }
}

此外,响应的 ErrorException 和 ErrorResponse 属性为 NULL。

这似乎是一个非常简单的案例,但我整天都在转圈!谢谢。

【问题讨论】:

  • 当你打电话给request.AddUrlSegment("singleItemTestId", singleItemTestId)而不是你必须打电话给AddParameter时会发生什么?

标签: c# json c#-4.0 restsharp


【解决方案1】:

响应中的Content-Type 是什么?如果不是“application/json”等标准内容类型,那么 RestSharp 将无法理解要使用哪个反序列化器。如果它实际上是 RestSharp 不“理解”的内容类型(您可以通过检查请求中发送的 Accept 来验证),那么您可以通过以下方式解决此问题:

client.AddHandler("my_custom_type", new JsonDeserializer());

编辑:

好的,抱歉,再次查看 JSON,您需要类似以下内容:

public class LocationResponse
   public LocationResult Result { get; set; }
}

public class LocationResult {
  public Location Location { get; set; }
}

然后做:

client.Execute<LocationResponse>(request);

【讨论】:

  • 内容类型为“application/json”。这行不应该:request.RootElement = "Location";消除对您建议的“LocationResponse”对象包装器的需求?
  • 嗯,在我尝试了您的第二次编辑建议后,它起作用了,但我不确定我理解为什么,除非我完全误解了 RootElement 属性的用途。嗯,谢谢!
  • JsonDeserializer 中的RootElement 支持似乎只支持将顶层对象上的属性指定为元素,例如JSON 中的“结果”。它不会对对象层次结构进行更深入的搜索:github.com/restsharp/RestSharp/blob/master/RestSharp/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多