Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize json array to c# list object

I am trying to parse a JSON response from a service to c# observation collection list object. The list object later can be used to showcase on the XAML page.

Here is the response from the service:

[
  {
    "orderId": 1,
    "employeeId": "6364",
    "orderTime": 1517583600000,
    "orderCost": 90,
    "comments": null,
    "orderStatus": {
      "orderStatusId": 1,
      "orderStatusName": "Order Placed"
    },
    "orderedItems": [
      {
        "orderItemId": 1,
        "orderQuantity": 1,
        "orderItemCost": 50
      },
      {
        "orderItemId": 2,
        "orderQuantity": 1,
        "orderItemCost": 40
      }
    ]
  },
  {
    "orderId": 2,
    "employeeId": "6364",
    "orderTime": 1517670000000,
    "orderCost": 50,
    "comments": null,
    "orderStatus": {
      "orderStatusId": 3,
      "orderStatusName": "Order Delivered"
    },
    "orderedItems": [
      {
        "orderItemId": 3,
        "orderQuantity": 1,
        "orderItemCost": 50
      }
    ]
  }
]

The following is the model class :

namespace ServiceNew
{

    public class OrderStatus
    {
        public int orderStatusId { get; set; }
        public string orderStatusName { get; set; }
    }

    public class OrderedItem
    {
        [JsonProperty("orderItemId")]
        public int orderItemId { get; set; }

        [JsonProperty("orderQuantity")]
        public int orderQuantity { get; set; }

        [JsonProperty("orderItemCost")]
        public int orderItemCost { get; set; }
    }

    public class Order
    {
        [JsonProperty("orderId")]
        public int orderId { get; set; }

        [JsonProperty("employeeId")]
        public string employeeId { get; set; }

        [JsonProperty("orderTime")]
        public object orderTime { get; set; }
        [JsonProperty("orderCost")]
        public int orderCost { get; set; }

        [JsonProperty("comments")]
        public object comments { get; set; }

        [JsonProperty("orderStatus")]
        public OrderStatus orderStatus { get; set; }

        [JsonProperty("orderedItems")]
        public List<OrderedItem> orderedItems { get; set; }
    }


}

The service is like this:

public  class OrderService
    {
        public OrderService()
        {
            GetJson();
        }
        public async void GetJson()
        {
            if (NetworkCheck.IsInternet())
            {
                var client = new System.Net.Http.HttpClient();
                var response = await client.GetAsync("here is thre URL");
                string orderJson = await response.Content.ReadAsStringAsync(); //Getting response  

                Order ObjOrderList = new Order();
                if (orderJson != " ")
                {

                    Console.WriteLine("response is"+orderJson);

                   //exception occurs here all the time , and I need it to be a list
                    ObjOrderList = JsonConvert.DeserializeObject<Order>(orderJson);
                }

                Console.WriteLine("obj order list is"+ObjOrderList);
            }
        }
    }

After trying with some changes to the deserialization the JSON array to c#, I was not able to succeed. Now there is an exception saying

Newtonsoft.Json.JsonSerializationException: <Timeout exceeded getting exception details>

And I am stuck at this for a long time, searched over StackOverflow and googled it but no fruitful solution for this.

I need to store the JSON data into a c# object and reproduce the same object in the XAML page as a list.

Thanks in advance!

like image 301
shitterpunk Avatar asked May 15 '18 05:05

shitterpunk


People also ask

How do I deserialize JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.


1 Answers

I am sure that exception is not related to you JSON string but try to remove bin and obj from solution folder and then clean and rebuild solution.

but after resolving that you will get the below exception

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'namespace.Order' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.....

Because your JSON string is List of Order so the deserialize would be change to :

List<Order> ObjOrderList = JsonConvert.DeserializeObject<List<Order>>(orderJson);

or in the other side you can also use JavaScriptSerializer like:

Order[] orderList = new JavaScriptSerializer().Deserialize<Order[]>(orderJson);
like image 79
Aria Avatar answered Oct 07 '22 09:10

Aria