Restsharp中json对象的反序列化
我有以下使用RestSharp调用web服务的代码。代码运行时没有错误,但是当我查看response.Data时,我发现Result对象没有反序列化,仍然包含默认值Restsharp中json对象的反序列化,restsharp,Restsharp,我有以下使用RestSharp调用web服务的代码。代码运行时没有错误,但是当我查看response.Data时,我发现Result对象没有反序列化,仍然包含默认值 <Serializable()> <DataContract()> _ Private Class Result <DataMember(IsRequired:=False)> _ Public responseCode As Long <DataMember(IsRequire
<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
反序列化运行得很好。
我试着调试代码的非工作版本的结果,内容看起来很好。这是我得到的内容:“{”responseMessage:“成功:您的请求已成功完成。”,“responseCode:2000}”,内容类型应该是“application/json”
我可以运行有效的代码,但是我很乐意知道为什么RestSharp的反序列化没有正确进行。RestSharp没有反序列化到字段,您必须将它们更改为属性,然后它应该可以工作
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