1
我有一些JSON我想反序列化(https://api.zype.com/consumers/?api_key=qfcR4WWqBws1ezeUHNxTTQl8ucuuP9pW1rMssmQBFUE-AbpPhKp_cY0RnfpY57y5):反序列化JSON的自定義類的列表
我有一個消費類和ZypeConsumerList:
Public Class Consumer
<JsonProperty("email")>
Public Property Email As String
<JsonProperty("stripe_id")>
Public Property StripeId As List(Of String)
<JsonProperty("_id")>
Public Property Id As String
<JsonProperty("_keywords")>
Public Property Keywords As List(Of String)
<JsonProperty("created_at")>
Public Property CreatedOn As Nullable(Of Date)
<JsonProperty("updated_at")>
Public Property UpdatedOn As Nullable(Of Date)
<JsonProperty("deleted_at")>
Public Property DeletedOn As Nullable(Of Date)
<JsonProperty("site_id")>
Public Property SiteId As String
<JsonProperty("subscription_count")>
Public Property SubscriptionCount As Int16
End Class
Public Class ZypeConsumerList
<JsonProperty("response")>
Public Property Consumers As List(Of DanceNetwork.Zype.Consumer)
<JsonProperty("pagination")>
Public Property Pagination As DanceNetwork.Zype.Pagination
End Class
我試圖反序列化該JSON:
Dim zResponse As ZypeResponse = myCall.Execute(ZypeRestEndpoint.Consumers)
Dim responseBlock As ZypeConsumerList
Dim mySerializerSettings As JsonSerializerSettings = New JsonSerializerSettings()
mySerializerSettings.NullValueHandling = NullValueHandling.Ignore
responseBlock = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ZypeConsumerList)(zResponse.data, mySerializerSettings)
我得到這個錯誤:
Error converting value "cus_AHtHxKXCwe6MPZ" to type 'System.Collections.Generic.List`1[System.String]'. Path 'response[3].stripe_id', line 1, position 2043.
我沒有看到任何地方的JSON格式不正確,上面的值實際上是一個字符串。我做錯了什麼,我該如何解決這個問題?
是不是stipe_id一個字符串?您將它定義爲班級中的List(Of String)。 – Lithium
爲什麼當你的代碼是VB時,你有這個標籤爲C#? –
我不知道你是如何創建這些類的,但我通常做的是去http://json2csharp.com/根據json結果生成類,以避免像你的那樣的錯誤。 –