我正在尝试构造POCO类,以便RestSharp可以反序列化特定的JSON结果。 关键点似乎是根对象包含nb_results键和其他键的字典(“0:”,“1:”等,每个键都有一个复数值)。
我尝试过Dictionary(Of Integer,mediaGalleryData)和Dictionary(Of String,mediaGalleryData)。两者都不起作用。 nb_results总是序列化,但字典永远不会。
{
"nb_results": 2,
"0": {
"id": 51976254,
"title": "Bumblebee species Bombus terrestris",
"media_type_id": 1,
"creator_name": "paulrommer",
"creator_id": 201446851,
"thumbnail_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/51\/97\/62\/110_F_51976254_dVCbgGVey5xEuLkvk1e4vhnmPqxI4J0X.jpg",
"thumbnail_html_tag": "<img src=\"http:\/\/t1.ftcdn.net\/jpg\/00\/51\/97\/62\/110_F_51976254_dVCbgGVey5xEuLkvk1e4vhnmPqxI4J0X.jpg\" alt=\"Bumblebee species Bombus terrestris\" title=\"Photo: Bumblebee species Bombus terrestris\" width=\"110\" height=\"73\" \/>",
"thumbnail_width": 110,
"thumbnail_height": 73,
"affiliation_link": "http:\/\/api.fotolia.com\/id\/51976254",
"thumbnail_30_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/51\/97\/62\/30_F_51976254_dVCbgGVey5xEuLkvk1e4vhnmPqxI4J0X.jpg",
"thumbnail_30_width": 30,
"thumbnail_30_height": 20,
"thumbnail_110_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/51\/97\/62\/110_F_51976254_dVCbgGVey5xEuLkvk1e4vhnmPqxI4J0X.jpg",
"thumbnail_110_width": 110,
"thumbnail_110_height": 73,
"thumbnail_400_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/51\/97\/62\/400_F_51976254_dVCbgGVey5xEuLkvk1e4vhnmPqxI4J0X.jpg",
"thumbnail_400_width": 400,
"thumbnail_400_height": 267,
"licenses": [
{
"name": "XS",
"price": 1
},
{
"name": "S",
"price": 3
},
{
"name": "M",
"price": 5
},
{
"name": "L",
"price": 7
},
{
"name": "XL",
"price": 8
},
{
"name": "XXL",
"price": 10
},
{
"name": "X",
"price": 100
}
]
},
"1": {
"id": 44488015,
"title": "Vintage Style Birds, Bees and Banners Vector Set",
"media_type_id": 3,
"creator_name": "artnerdluxe",
"creator_id": 203491263,
"thumbnail_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/44\/48\/80\/110_F_44488015_hvEpYPw8yILDsnAi6BChYWXtzmiH6jWd.jpg",
"thumbnail_html_tag": "<img src=\"http:\/\/t1.ftcdn.net\/jpg\/00\/44\/48\/80\/110_F_44488015_hvEpYPw8yILDsnAi6BChYWXtzmiH6jWd.jpg\" alt=\"Vintage Style Birds, Bees and Banners Vector Set\" title=\"Vector: Vintage Style Birds, Bees and Banners Vector Set\" width=\"105\" height=\"110\" \/>",
"thumbnail_width": 105,
"thumbnail_height": 110,
"affiliation_link": "http:\/\/api.fotolia.com\/id\/44488015",
"thumbnail_30_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/44\/48\/80\/30_F_44488015_hvEpYPw8yILDsnAi6BChYWXtzmiH6jWd.jpg",
"thumbnail_30_width": 29,
"thumbnail_30_height": 30,
"thumbnail_110_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/44\/48\/80\/110_F_44488015_hvEpYPw8yILDsnAi6BChYWXtzmiH6jWd.jpg",
"thumbnail_110_width": 105,
"thumbnail_110_height": 110,
"thumbnail_400_url": "http:\/\/t1.ftcdn.net\/jpg\/00\/44\/48\/80\/400_F_44488015_hvEpYPw8yILDsnAi6BChYWXtzmiH6jWd.jpg",
"thumbnail_400_width": 380,
"thumbnail_400_height": 400,
"licenses": [
{
"name": "XS",
"price": 1
},
{
"name": "S",
"price": 3
},
{
"name": "M",
"price": 5
},
{
"name": "L",
"price": 7
},
{
"name": "XL",
"price": 8
},
{
"name": "XXL",
"price": 10
},
{
"name": "V",
"price": 5
},
{
"name": "XV",
"price": 40
}
]
}
}
以下是我到目前为止的课程:
Public Class mediaGallery
Public Property nb_results As Integer
Public Property results As Dictionary(Of String, mediaGalleryData)
End Class
Public Class mediaGalleryData
Public Property id As Integer
Public Property title As String
Public Property creator_name As String
Public Property creator_id As Integer
Public Property media_type_id As Integer
Public Property thumbnail_url As String
Public Property thumbnail_width As Integer
Public Property thumbnail_height As Integer
Public Property thumbnail_html_tag As String
Public Property thumbnail_30_url As String
Public Property thumbnail_30_width As Integer
Public Property thumbnail_30_height As Integer
Public Property thumbnail_110_url As String
Public Property thumbnail_110_width As Integer
Public Property thumbnail_110_height As Integer
Public Property thumbnail_400_url As String
Public Property thumbnail_400_width As Integer
Public Property thumbnail_400_height As Integer
Public Property licenses As List(Of licensedata)
End Class
Public Class licensedata
Public Property name As String
Public Property price As Integer
End Class
我的RestSharp消费代码:
Public Function getUserGalleryMedias(page As Integer?, nb_per_page As Integer?, thumbnail_size As Integer?, id As String, detail_level As Integer?) As mediaGallery
Dim request = New RestRequest("user/getUserGalleryMedias", Method.GET)
If page.HasValue Then request.AddParameter("page", page.Value)
If nb_per_page.HasValue Then request.AddParameter("nb_per_page", nb_per_page.Value)
If thumbnail_size.HasValue Then request.AddParameter("thumbnail_size", thumbnail_size.Value)
If Not String.IsNullOrEmpty(id) Then request.AddParameter("id", id)
If detail_level.HasValue Then request.AddParameter("detail_level", detail_level.Value)
Return Execute(Of mediaGallery)(request)
End Function
答案 0 :(得分:1)
我已经完成了我需要做的事情,但我不得不走了很长一段路。
正如wiki上RestSharp documentation所建议的那样(但没有很好地解释),我实现了IDeserializer
来专门处理这个类。我必须单独将SimpleJson
添加到网站,因为它在RestSharp中标记为Friend
。而不是将我的反序列化程序注册为RestClient的处理程序,而这需要我的反序列化程序来处理所有类,我只是在我的API类的Execute(Of T)
方法中调用它,对于单个情况,其中T是麻烦的类
Execute(Of T)
方法:
Public Function Execute(Of T As New)(request As RestRequest) As T
Dim client = New RestClient()
' set up client with authenticator
client.BaseUrl = BaseURL
If String.IsNullOrEmpty(_sessionID) Then
client.Authenticator = New HttpBasicAuthenticator(_apikey, "")
Else
client.Authenticator = New HttpBasicAuthenticator(_apikey, _sessionID)
End If
' used on every request
Dim response = client.Execute(Of T)(request)
' Check for exceptions or bad status code
If response.ErrorException IsNot Nothing Then
Throw response.ErrorException
ElseIf response.StatusCode > 299 Then
If response.Content.StartsWith("{""error""") Then
Throw New Exception(String.Format("Status {0} {1} : {2}", response.StatusCode, response.StatusDescription, response.Content))
Else
Throw New HttpException(response.StatusCode, response.StatusDescription)
End If
End If
' Handle troublesome class
If GetType(T).Equals(GetType(mediaGallery)) Then
Dim fjs As New fotoliaJSONDeSerializer
response.Data = fjs.Deserialize(Of T)(response)
End If
Return response.Data
End Function
反序列化程序类:
''' <summary>
''' specific to the FotoliaAPI.mediaGallery we actually want to handle
''' </summary>
''' <param name="response"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function DeserializeMediaLibrary(response As IRestResponse) As FotoliaAPI.mediaGallery
Dim result As New FotoliaAPI.mediaGallery
' turn to an iDictionary so we can readily enumerate keys
Dim j As IDictionary(Of String, Object) = CType(SimpleJSON.SimpleJson.DeserializeObject(response.Content), IDictionary(Of String, Object))
For Each key In j.Keys
' this top level key is a direct property
If key = "nb_results" Then
result.nb_results = j(key)
Else
' all other keys become members of the list.
' Turn the value back to a string, then feed it back into SimpleJson to deserialize to the strongly typed subclass.
result.media.Add( _
SimpleJSON.SimpleJson.DeserializeObject(Of FotoliaAPI.mediaGalleryData)( _
j(key).ToString))
End If
Next
Return result
End Function
' required but unimportant properties of IDeserializer
Public Property DateFormat As String Implements Deserializers.IDeserializer.DateFormat
Public Property [Namespace] As String Implements Deserializers.IDeserializer.Namespace
Public Property RootElement As String Implements Deserializers.IDeserializer.RootElement
End Class