0

逆シリアル化/シリアル化してデータ コントラクトを 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"
    }
}

これは私が得たものです。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)
4

1 に答える 1

0

の値"basket_items"は JSON 配列で、括弧で囲まれた値のリストです: [value1, value2, ..., valueN]. ドキュメントによると、DataContractJsonSerializer「コレクション、辞書、および配列」を 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 で配列値プロパティを定義します。

    Public Property basket_items As Basket_Items()  

詳細はこちら

于 2015-11-19T06:43:29.353 に答える