我有以下使用RestSharp来调用Web服务的代码。代码运行时没有错误,但是当我查看response.Data时,我看到Result对象没有反序列化,仍然包含默认值。
<Serializable()> <DataContract()> _
Private Class Result
<DataMember(IsRequired:=False)> _
Public responseCode As Long
<DataMember(IsRequired:=False)> _
Public responseMessage As String
Friend Const RESULT_OK As Long = 2000
End Class
Private Function Login(ByVal user As String, ByVal password As String) As RestResponseCookie
Dim req As New RestRequest("authenticationService/authentication/signIn", Method.POST)
req.RequestFormat = DataFormat.Json
Dim input As New SigninInput
input.requestParameters.username = user
input.requestParameters.password = password
req.AddBody(input)
Dim response As RestResponse(Of Result) = getRestClient().Execute(Of Result)(req)
If response.StatusCode = HttpStatusCode.OK Then
Dim res As Result = response.Data
End If
Return Nothing
End Function
现在,如果我将代码更改为以下内容:
Dim response As RestResponse = getRestClient().Execute(req)
If response.StatusCode = HttpStatusCode.OK Then
Dim des As New Json.DataContractJsonSerializer(GetType(Result))
Dim res As Result = CType(des.ReadObject(New IO.MemoryStream(response.RawBytes)), Result)
End If
反序列化运行得很好。 我试图从代码的非工作版本调试结果,内容似乎没问题。这是我得到的内容:“{”responseMessage“:”成功:您的请求已成功完成。“,”responseCode“:2000}”和ContentType是“application / json”应该是。
我可以运行有效的代码,但我很高兴知道为什么RestSharp的反序列化没有正确发生。
答案 0 :(得分:1)
RestSharp不会反序列化为字段,您必须将它们更改为属性,然后它才能正常工作。