Feroz Khan

Feroz Khan

  • 1.4k
  • 300
  • 92.7k

Json Array convert to Data table

Nov 8 2023 10:16 PM

Dear All, i am using api and there is data coming in Json i need to convert into Datatable and i created public class as well and trying to desrilized but not working. please need your support.

below my code and json format.

 

var response = await client.SendAsync(request);
                response.EnsureSuccessStatusCode();

                string responseData = await response.Content.ReadAsStringAsync();
                var data = JsonConvert.DeserializeObject<Datum>(responseData);
 

public class Branch
        {
            public string id { get; set; }
            public Name name { get; set; }
        }
       // private DateTime date = "2023-10-23";
        public class Datum
        {
            public string id { get; set; }
            public string type { get; set; }
            public string origin { get; set; }
            public string documentNumber { get; set; }
            public DateTime documentDate { get; set; }
            public string currency { get; set; }
            public Supplier supplier { get; set; }
            public Retailer retailer { get; set; }
            public Location location { get; set; }
            public Branch branch { get; set; }
            public List<Item> items { get; set; }
            public Totals totals { get; set; }
        }

        public class Item
        {
            public ItemCategory itemCategory { get; set; }
            public double lineAmount { get; set; }
            public int discount { get; set; }
            public double subTotal { get; set; }
            public int taxRate { get; set; }
            public string taxCode { get; set; }
            public double taxAmount { get; set; }
            public double total { get; set; }
        }

        public class ItemCategory
        {
            public string id { get; set; }
            public Name name { get; set; }
        }

        public class Location
        {
            public string id { get; set; }
            public Name name { get; set; }
        }

        public class Metadata
        {
            public int count { get; set; }
            public int total { get; set; }
        }

        public class Name
        {
            public string en { get; set; }
            public object ar { get; set; }
        }

        public class Retailer
        {
            public string id { get; set; }
            public Name name { get; set; }
        }

        public class Root
        {
            public List<Datum> data { get; set; }
            public Metadata metadata { get; set; }
        }

        public class Supplier
        {
            public string id { get; set; }
            public Name name { get; set; }
        }

        public class Totals
        {
            public double taxAmount { get; set; }
            public double subTotal { get; set; }
            public double total { get; set; }
        }

 

 

Json file

{

    "data": [

        {

            "id": "653f7b98175871ae0da687b0",

            "type": "invoice",

            "origin": "purchase",

            "documentNumber": "2023/10/00218",

            "documentDate": "2023-10-28T14:00:06.805Z",

            "currency": "SAR",

            "supplier": {

                "id": "6537a057fc7d89c5400713ef",

                "name": {

                    "en": "Supplier Name"

                }

            },

            "retailer": {

                "id": "64e4527cc78560ea7f3bb2b8",

                "name": {

                    "en": "Company Name"

                }

            },

            "location": {

                "id": "64e4527cc78560ea7f3bb2c1",

                "name": {

                    "en": "Food"

                }

            },

            "branch": {

                "id": "64e4527cc78560ea7f3bb2ba",

                "name": {

                    "en": "ABC",

                    "ar": null

                }

            },

            "items": [

                {

                    "itemCategory": {

                        "id": "64e477e4b5958a0428e41440",

                        "name": {

                            "en": "Meat",

                            "ar": null

                        }

                    },

                    "lineAmount": 684.552,

                    "discount": 0,

                    "subTotal": 684.552,

                    "taxRate": 15,

                    "taxCode": "VAT15",

                    "taxAmount": 102.6828,

                    "total": 787.2348000000001

                }

            ],

            "totals": {

                "taxAmount": 102.6828,

                "subTotal": 684.552,

                "total": 787.2348000000001

            }

        },

 


Answers (6)