0

I am using ASP.Net MVC and JSON.Net API. I would like to deserialize two objects, let's say: CustomerOrderHeader and a CustomerOrderLines collection. I have the JSON in the jsonString variable:

 {
    "customerOrderHeader": {
        "OrderNumber": "",
        "CustomerName": "",
        "Reference": "",
        "ShippingDate": "",
        "CurrencyId": "0",
        "LanguageId": "0",
        "DaLine1": "",
        "DaLine2": "",
        "DaCity": "",
        "DaPostalCode": "",
        "DaState": "",
        "DaCountry": "",
        "BaLine1": "",
        "BaLine2": "",
        "BaCity": "",
        "BaPostalCode": "",
        "BaState": "",
        "BaCountry": "",
        "Notes": ""
    },
    "customerOderLinesModel": [
        {
            "Num": "10",
            "PartNumber": "Inventory1",
            "Description": "aasss",
            "OrderedQuantity": "0",
            "UoM": "cm",
            "SalesPrice": "123.0000",
            "Amount": "0.0000",
            "LineStatus": "Draft",
            "ConfirmedShippingDate": "",
            "ReservedQuantity": "0",
            "DeliveredQuantity": "0",
            "RemainingQuantity": "0"
        }
    ]
}

And I have the Classes:

CustomerOrderModel {

        public string BillingAddress
        { get; set; }

        public string ShippingAddress
        { get; set; }

        [DisplayName("Customer name*:")]
        [Required]
        public string CustomerName
        { get; set; }

        [DisplayName("Order Number :")]
        public long? OrderNumber
        { get; set; }

        [DisplayName("Order Status :")]
        public int OrderStatus
        { get; set; }

        public string OrderStatusName 
        { get; set; }

        [DisplayName("Shipping Date :")]
        public DateTime? ShippingDate
        { get; set; }

        public bool IsDefaultda
        { get; set; }  

        [DisplayName("Total Amount :")]
        public decimal TotalAmount
        { get; set; }

        [DisplayName("Address Line 1 :")]
        public string DaLine1
        { get; set; }

        [DisplayName("Address Line 2 :")]
        public string DaLine2
        { get; set; }

        [DisplayName("City :")]
        public string DaCity
        { get; set; }

        [DisplayName("Postal Code :")]
        public string DaPostalCode
        { get; set; }

        [DisplayName("State :")]
        public string DaState
        { get; set; }

        [DisplayName("Country :")]
        public string DaCountry
        { get; set; }

        [DisplayName("Address Line 1 :")]
        public string BaLine1
        { get; set; }

        [DisplayName("Address Line 2 :")]
        public string BaLine2
        { get; set; }

        [DisplayName("City :")]
        public string BaCity
        { get; set; }

        [DisplayName("Postal Code :")]
        public string BaPostalCode
        { get; set; }

       ...
}

and

public class CustomerOrderLineModel
    {
        public CustomerOrderLineModel()
        {
        }

        public String OrderNumber { get; set; }
        public String Num { get; set; }

        public String PartNumber { get; set; }
        public String Description { get; set; }
        public String OrderedQuantity { get; set; }
        public String UoM { get; set; }
        public String SalesPrice { get; set; }
        public String Amount { get; set; }
        public String LineStatus { get; set; }
        public DateTime? ConfirmedShippingDate { get; set; }
        public String ReservedQuantity { get; set; }
        public String DeliveredQuantity { get; set; }
        public String RemainingQuantity { get; set; }


    }

What I just want to do is get the JSON and pass to this classes:

  JObject jObject = JObject.Parse(jsonString);
  JToken jToken =  jObject.SelectToken("customerOrderHeader");

Now I would like to make this jToken PassTo CustomerOrderModel class, Something like:

 CustomerOrderModel customerOrderModel = (CustomerOrderModel) jObject.SelectToken("customerOrderHeader");

the same to the collection of CustomerOrderLineModels:

List<CustomerOrderLineModel> customerOrderLines = (List<CustomerOrderLineModel>) jObject.SelectToken("customerOderLinesModel");

But I can't do this, because it returns string.

thanks,

Tito

1 Answer 1

0

You need to deserialize it. http://json.codeplex.com/ is the serializer I always use. It is very flexible and fast. You call it something like this:

CustomerOrderModel customerOrderModel =
  json.Deserialize<CustomerOrderModel>(jObject.SelectToken("customerOrderHeader"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.