DataContract Json 项目集合中的序列化程序和缺少括号
DataContract Json Serializer and missing bracket in collection of items
当我 de-serialize/serialize 和数据合同为 json 格式时,我的项目集合缺少括号,导致在发布到网络 api 时失败。
这是带有 [] 方括号
的正确格式的 json
{
"basket": {
"basket_platform_type": "in_store",
"basket_currency": {
"currency_id": 2,
"currency_code": "ZAR"
},
"basket_items": [
{
"spaaza_product_id": 18605,
"retailer_product_code": "WBGT0234",
"retailer_item_code": "line_0",
"item_quantity": 3,
"item_price": 250
}
],
"retailer_basket_code": "70401",
"basket_total_price": 750
},
"entity": {
"entity_type": "chain",
"entity_id": 1740,
"branch_business_owner_code": "501",
"branch_business_id": 1341
},
"user": {
"member_programme": "spaaza",
"member_number": "33017307"
}
}
这就是我得到的,我在 basketitems
缺少 []
{
"basket": {
"basket_platform_type": "in_store",
"basket_currency": {
"currency_id": 2,
"currency_code": "ZAR"
},
"basket_items":
{
"spaaza_product_id": 18605,
"retailer_product_code": "WBGT0234",
"retailer_item_code": "line_0",
"item_quantity": 3,
"item_price": 250
},
"retailer_basket_code": "70401",
"basket_total_price": 750
},
"entity": {
"entity_type": "chain",
"entity_id": 1740,
"branch_business_owner_code": "501",
"branch_business_id": 1341
},
"user": {
"member_programme": "spaaza",
"member_number": "33017307"
}
}
这是 类 以及我用于序列化的函数。
Namespace Global.MyPrice
Public Class GetBasketPrice
Public Class Entity
Public Property entity_type As String
Public Property entity_id As Integer
Public Property branch_business_owner_code As String
Public Property branch_business_id As Integer
End Class
Public Class User
Public Property member_programme As String
Public Property member_number As String
End Class
Public Class Basket_Currency
Public Property currency_id As Integer
Public Property currency_code As String
End Class
Public Class Rootobject
Public Property basket As Basket
Public Property entity As Entity
Public Property user As User
End Class
Public Class Basket_Items
Public Property spaaza_product_id As Integer
Public Property retailer_product_code As String
Public Property retailer_item_code As String
Public Property item_quantity As Integer
Public Property item_price As Single
End Class
Public Class Basket
Public Property basket_platform_type As String
Public Property basket_currency As Basket_Currency
Public Property basket_items() As Basket_Items
Public Property retailer_basket_code As String
Public Property basket_total_price As Single
End Class
End Class
结束命名空间
这是序列化函数
Dim jsonstring As String
Dim stream1 As New MemoryStream()
Dim ser As New DataContractJsonSerializer(GetType(MyPrice.GetBasketPrice.Rootobject))
ser.WriteObject(stream1, MyPriceBasket)
stream1.Position = 0
Dim sr As New StreamReader(stream1)
Console.Write("JSON form of Person object: ")
jsonstring = sr.ReadToEnd()
Console.WriteLine(jsonstring)
"basket_items"
的值是一个 JSON 数组,它是一个带括号的值列表:[value1, value2, ..., valueN]
。根据 documentation,DataContractJsonSerializer
将 "Collections, dictionaries, and arrays" 映射到 JSON 数组。因此,您的 basket_items
属性 需要是某种集合,例如 List(Of Basket_Items)
:
Public Class Basket
Public Property basket_platform_type As String
Public Property basket_currency As Basket_Currency
Public Property basket_items As List(Of Basket_Items)
Public Property retailer_basket_code As String
Public Property basket_total_price As Single
End Class
或者,如果您想使用数组而不是列表,则您的 ()
位置错误。您在 VB.NET like this:
中定义数组值 属性
Public Property basket_items As Basket_Items()
更多 here.
当我 de-serialize/serialize 和数据合同为 json 格式时,我的项目集合缺少括号,导致在发布到网络 api 时失败。 这是带有 [] 方括号
的正确格式的 json{
"basket": {
"basket_platform_type": "in_store",
"basket_currency": {
"currency_id": 2,
"currency_code": "ZAR"
},
"basket_items": [
{
"spaaza_product_id": 18605,
"retailer_product_code": "WBGT0234",
"retailer_item_code": "line_0",
"item_quantity": 3,
"item_price": 250
}
],
"retailer_basket_code": "70401",
"basket_total_price": 750
},
"entity": {
"entity_type": "chain",
"entity_id": 1740,
"branch_business_owner_code": "501",
"branch_business_id": 1341
},
"user": {
"member_programme": "spaaza",
"member_number": "33017307"
}
}
这就是我得到的,我在 basketitems
缺少 []{
"basket": {
"basket_platform_type": "in_store",
"basket_currency": {
"currency_id": 2,
"currency_code": "ZAR"
},
"basket_items":
{
"spaaza_product_id": 18605,
"retailer_product_code": "WBGT0234",
"retailer_item_code": "line_0",
"item_quantity": 3,
"item_price": 250
},
"retailer_basket_code": "70401",
"basket_total_price": 750
},
"entity": {
"entity_type": "chain",
"entity_id": 1740,
"branch_business_owner_code": "501",
"branch_business_id": 1341
},
"user": {
"member_programme": "spaaza",
"member_number": "33017307"
}
}
这是 类 以及我用于序列化的函数。
Namespace Global.MyPrice
Public Class GetBasketPrice
Public Class Entity
Public Property entity_type As String
Public Property entity_id As Integer
Public Property branch_business_owner_code As String
Public Property branch_business_id As Integer
End Class
Public Class User
Public Property member_programme As String
Public Property member_number As String
End Class
Public Class Basket_Currency
Public Property currency_id As Integer
Public Property currency_code As String
End Class
Public Class Rootobject
Public Property basket As Basket
Public Property entity As Entity
Public Property user As User
End Class
Public Class Basket_Items
Public Property spaaza_product_id As Integer
Public Property retailer_product_code As String
Public Property retailer_item_code As String
Public Property item_quantity As Integer
Public Property item_price As Single
End Class
Public Class Basket
Public Property basket_platform_type As String
Public Property basket_currency As Basket_Currency
Public Property basket_items() As Basket_Items
Public Property retailer_basket_code As String
Public Property basket_total_price As Single
End Class
End Class
结束命名空间
这是序列化函数
Dim jsonstring As String
Dim stream1 As New MemoryStream()
Dim ser As New DataContractJsonSerializer(GetType(MyPrice.GetBasketPrice.Rootobject))
ser.WriteObject(stream1, MyPriceBasket)
stream1.Position = 0
Dim sr As New StreamReader(stream1)
Console.Write("JSON form of Person object: ")
jsonstring = sr.ReadToEnd()
Console.WriteLine(jsonstring)
"basket_items"
的值是一个 JSON 数组,它是一个带括号的值列表:[value1, value2, ..., valueN]
。根据 documentation,DataContractJsonSerializer
将 "Collections, dictionaries, and arrays" 映射到 JSON 数组。因此,您的 basket_items
属性 需要是某种集合,例如 List(Of Basket_Items)
:
Public Class Basket
Public Property basket_platform_type As String
Public Property basket_currency As Basket_Currency
Public Property basket_items As List(Of Basket_Items)
Public Property retailer_basket_code As String
Public Property basket_total_price As Single
End Class
或者,如果您想使用数组而不是列表,则您的 ()
位置错误。您在 VB.NET like this:
Public Property basket_items As Basket_Items()
更多 here.