0

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?

wazz
  • 4,953
  • 5
  • 20
  • 34
andy
  • 1
  • 1
    Possible duplicate of [How to deserialize a property with a dash (“-”) in it's name with NewtonSoft JsonConvert?](https://stackoverflow.com/questions/14753113/how-to-deserialize-a-property-with-a-dash-in-its-name-with-newtonsoft-jso) – Jonatan Dragon Jun 23 '18 at 14:24
  • Not sure if duplicate. Looks like Visual Basic code, not C#. – Sebastian D'Agostino Jun 23 '18 at 14:35
  • its similar and can see the solution for property but not for the object name...how is this done in c# or vb.net ? – andy Jun 23 '18 at 15:22
  • The duplicate applies. It is the same issue and the answer can be applied to this question. Use the JsonProperty attribute. – Nkosi Jun 23 '18 at 22:04

0 Answers0