【问题标题】:DataContract Json Serializer and missing bracket in collection of itemsDataContract Json 序列化程序和项目集合中缺少括号
【发布时间】:2015-11-19 06:21:39
【问题描述】:

当我反序列化/序列化并将数据合同转换为 json 格式时,我的项目集合缺少括号,并导致它在发布到 web 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"
    }
}

这就是我得到的,我在购物篮中缺少 []

{
    "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)

【问题讨论】:

  • C#在哪里?您正在使用 VB

标签: json vb.net serialization datacontractjsonserializer


【解决方案1】:

"basket_items" 的值是一个 JSON 数组,它是一个带括号的值列表:[value1, value2, ..., valueN]。根据documentationDataContractJsonSerializer 将“集合、字典和数组”映射到 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多