【发布时间】:2019-03-18 12:18:53
【问题描述】:
我正在开发一个系统,它将向 VB.NET 中的 Web API 发送 POST 或 GET 请求。
我正在使用 Newtonsoft.Json 将返回的 JSON 字符串转换为 VB 对象。
尝试反序列化响应时出现以下错误。
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'ProjectName.Customer[]',因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化. 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径“customers.id”,第 1 行,位置 19。
API 返回给我的 JSON 字符串是:
{"customers":{"id":"CU0004FEY6D7HA","created_at":"2018-10-13T13:30:21.320Z","email":"test@test.com","given_name": "Joe","family_name":"Bloggs","company_name":null,"address_line1":"1 街道名称","address_line2":"","address_line3":null,"city":"伦敦"," region":null,"postal_code":"SW1A 1AA","country_code":"GB","language":"en","swedish_identity_number":null,"danish_identity_number":null,"phone_number":null,"metadata ":{}}}
我已经为对象创建了一个类。
VB.NET 不是我最擅长的,因为我主要使用 PHP。
谁能提供有用的建议?
Public Class CustomerWrapper
Public customers() As Customer
End Class
Public Class Metadata
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = value
End Set
End Property
Private m_id As String
End Class
Public Class Customer
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = value
End Set
End Property
Private m_id As String
Public Property created_at() As String
Get
Return m_created_at
End Get
Set(value As String)
m_created_at = value
End Set
End Property
Private m_created_at As String
Public Property email() As String
Get
Return m_email
End Get
Set(value As String)
m_email = value
End Set
End Property
Private m_email As String
Public Property given_name() As String
Get
Return m_given_name
End Get
Set(value As String)
m_given_name = value
End Set
End Property
Private m_given_name As String
Public Property family_name() As String
Get
Return m_family_name
End Get
Set(value As String)
m_family_name = value
End Set
End Property
Private m_family_name As String
Public Property address_line1() As String
Get
Return m_address_line1
End Get
Set(value As String)
m_address_line1 = value
End Set
End Property
Private m_address_line1 As String
Public Property address_line2() As String
Get
Return m_address_line2
End Get
Set(value As String)
m_address_line2 = value
End Set
End Property
Private m_address_line2 As String
Public Property address_line3() As String
Get
Return m_address_line3
End Get
Set(value As String)
m_address_line3 = value
End Set
End Property
Private m_address_line3 As String
Public Property city() As String
Get
Return m_city
End Get
Set(value As String)
m_city = value
End Set
End Property
Private m_city As String
Public Property region() As String
Get
Return m_region
End Get
Set(value As String)
m_region = value
End Set
End Property
Private m_region As String
Public Property postal_code() As String
Get
Return m_postal_code
End Get
Set(value As String)
m_postal_code = value
End Set
End Property
Private m_postal_code As String
Public Property country_code() As String
Get
Return m_country_code
End Get
Set(value As String)
m_country_code = value
End Set
End Property
Private m_country_code As String
Public Property language() As String
Get
Return m_language
End Get
Set(value As String)
m_language = value
End Set
End Property
Private m_language As String
Public Property phone_number() As String
Get
Return m_phone_number
End Get
Set(value As String)
m_phone_number = value
End Set
End Property
Private m_phone_number As String
Public metadata() As Metadata
End Class
调用Json的代码是
Dim response = apiCall.CallApi()
'Dim customerId = customers("id")
Dim customerWrapper = JsonConvert.DeserializeObject(Of CustomerWrapper)(response)
Dim customers = customerWrapper.customers
【问题讨论】:
-
在
Public Property customers() As Customer中更改CustomerWrappercustomers()声明。您不需要那些支持字段。使用自动属性(例如,Public Property id() As String、Public Property family_name() As String等,仅此而已。)您还应该使用JsonProperty属性(例如,<JsonProperty("family_name")> Public Property FamilyName() As String)。