2

When I de-serialize/serialize and data contract to a json format my collection of items is missing the bracket and is causing it fail when posting to web api. Here is the json in the correct format with [] brackets

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

This is what I get, I am missing the [] at 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"
    }
}

Here is classes and the functions I am using for serialization.

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

End Namespace

This is the serialization function

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)
dbc
  • 104,963
  • 20
  • 228
  • 340
Etienne
  • 179
  • 3
  • 12

1 Answers1

1

The value for "basket_items" is a JSON array, which is a bracketed list of values: [value1, value2, ..., valueN]. According to the documentation, DataContractJsonSerializer maps "Collections, dictionaries, and arrays" to a JSON array. Thus your basket_items property needs to be a collection of some sort, for instance a 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

Or, if you want to use an array rather than a list, your () is in the wrong location. You define an array-valued property in VB.NET like this:

    Public Property basket_items As Basket_Items()  

More here.

dbc
  • 104,963
  • 20
  • 228
  • 340