how to deserialise json string when the json contains a - character on the name

50 views Asked by At

I'm being asked to read in orders from json same format as http://jsonapi.org/

Here is a sample, note that some of the names have "-" in them for example: order-totals, order-products

"data": [
    {
        "type": "orders",
        "attributes": {
            "customer_address_name": "testTestmann",
            "customer_address_company": "",
            "customer_address_zipcode": "9022",
            "customer_address_state": "uk",
        },
        "relationships": {
            "customer": {
                "data": {
                    "type": "customers",
                    "id": "1"
                }
            },
            "order-status": {
                "data": {
                    "type": "order-status",
                    "id": "3"
                }
            },
            "order-totals": {
                "links": {
                    "related": "test2"
                }
            },
            "order-products": {
                "links": {
                    "related": "test1"
                }
            },
            "order-tags": {
                "links": {
                    "related": "test3"
                }
            },
            "order-status-history": {
                "links": {
                    "related": "test4"
                }
            }
        },

Here is the code I use to deserialise:

Dim jsonOrders As String = jutils.GetJsonText(shopurl, shoptoken)
Dim obj = JsonConvert.DeserializeObject(Of MYS_Orders.MyStore_Orders)(jsonOrders)

This works fine on all attributes apart from the ones with names that include a "-" so of course OrderStatus does not deserialise.

This is what the objects look like :

Public Class OrderStatus
    Public Property data As Data
End Class

Public Class OrderTotals
    Public Property links As Links
End Class

Public Class OrderProducts
    Public Property links As Links
End Class

How can I name the objects with a "-" character so the deserialization works?

0

There are 0 answers