【发布时间】:2017-04-20 16:26:31
【问题描述】:
我有一些 JSON 正在尝试反序列化 (https://api.zype.com/consumers/?api_key=qfcR4WWqBws1ezeUHNxTTQl8ucuuP9pW1rMssmQBFUE-AbpPhKp_cY0RnfpY57y5):
我有一个 Consumer 类和 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)。
-
当你的代码是 VB 时,为什么你把它标记为 C#?
-
我不知道你是如何创建这些类的,但我通常会去json2csharp.com根据json结果生成类,以避免像你这样的错误。跨度>