none
Deserialize partial json to c# object RRS feed

  • Question

  • I have customer data in Json and there will be 3 or 4 addresses associated to each customer. The parameter name is diff for each address but the class structure remains same.

    that is shipping and billing address will have same structure for address and phone number. So instead of creating diff address class like billingaddress and shippingaddress.

    Can we create a single class Address which will b used by all.

    I tried with creating custom jsonconverter. But the TOObject() ends up calling readjson method and it goes in infinite loop. A sample code snippet would help.

    Thanks.

    Below is my sample Json.

    {
    "CustomerName": "ABC",
    "Sex": "Female",
    "Samedaydelivery": "Y",
    "OrderPrice": "123.12",
    "addressinfo": {
    "maincust": {
    "maincustaddress": [{
    "Name": "XXX",
    "Addressline1": "flatnumber",
    "Addressline2": "Streetname",
    "city": "London",
    "zipcode": "123123"
    }],
    "Maincontact": [{
    "firstname": "Q",
    "lastname": "A",
    "contacts": [{
    "type": "EMAIL",
    "email": "test@test.com",
    "number": null
    }, {

    "number": "1231231231",
    "type": "PHONE",
    "email": null
    }]
    }]
    },
    "billinginfo": {
    "billaddress": [{
    "Name": "XXX",
    "Addressline1": "flatnumber",
    "Addressline2": "Streetname",
    "city": "London",
    "zipcode": "123123"
    }],
    "billingcontact": [{
    "firstname": "Q",
    "lastname": "A",
    "contacts": [{
    "type": "EMAIL",
    "email": "test@test.com",
    "number": null
    }, {

    "number": "1231231231",
    "type": "PHONE",
    "email": null
    }]
    }]
    },
    "shippinginfo": {
    "shippingaddress": [{
    "Name": "XXX",
    "Addressline1": "flatnumber",
    "Addressline2": "Streetname",
    "city": "London",
    "zipcode": "123123"
    }],
    "ship_contact": [{
    "firstname": "A",
    "lastname": "Z",
    "contacts": [{
    "type": "EMAIL",
    "email": "test@test.com",
    "number": null
    }, {

    "number": "1231231231",
    "type": "PHONE",
    "email": null
    }]
    }]
    }
    }
    }

    Tuesday, December 6, 2016 4:32 PM

All replies

  • Use http://json2csharp.com/ to create c# class rom json
    Wednesday, December 7, 2016 4:05 AM
  • Hi Prahalnathan,

    Thank you for posting here.

    Please try the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    
    namespace deserializeJsonFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                string JSON = @"{
                'CustomerName': 'ABC',
                'Sex': 'Female',
                'Samedaydelivery': 'Y',
                'OrderPrice': '123.12',
                'addressinfo': {
                                'maincust': {
                                             'maincustaddress': [{
                                                                  'Name': 'XXX',
                                                                  'Addressline1': 'flatnumber',
                                                                  'Addressline2': 'Streetname',
                                                                  'city': 'London',
                                                                  'zipcode': '123123'}],
                                             'Maincontact': [{
                                                              'firstname': 'Q',
                                                              'lastname': 'A',
                                                              'contacts': [{'type': 'EMAIL','email': 'test@test.com','number': null}, 
                                                                           {'number': '1231231231','type': 'PHONE','email': null}]}]},
                                'billinginfo': {
                                                'billaddress': [{
                                                                 'Name': 'XXX',
                                                                 'Addressline1': 'flatnumber',
                                                                 'Addressline2': 'Streetname',
                                                                 'city': 'London',
                                                                 'zipcode': '123123'}],
                                                'billingcontact': [{
                                                                    'firstname': 'Q',
                                                                    'lastname': 'A',
                                                                    'contacts': [{'type': 'EMAIL','email': 'test@test.com','number': null}, 
                                                                                 {'number': '1231231231','type': 'PHONE','email': null}]}]},
                                'shippinginfo': {
                                                 'shippingaddress': [{'Name': 'XXX','Addressline1': 'flatnumber','Addressline2': 'Streetname','city': 'London','zipcode': '123123'}],
                                                 'ship_contact': [{'firstname': 'A','lastname': 'Z','contacts': [{'type': 'EMAIL','email': 'test@test.com','number': null},
                                                                                                                 {'number': '1231231231','type': 'PHONE','email': null}]}]}}}";
                Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(JSON);
                foreach (var item in obj.addressinfo.billinginfo.billaddress)
                {
                    Console.WriteLine("Name:{0},Addressline1:{1},Addressline2:{2},city:{3},zipcode:{4}", item.Name,item.Addressline1,item.Addressline2,item.city,item.zipcode);
                }          
                Console.ReadKey();
            }
        }
    
        public class Rootobject
        {
            public string CustomerName { get; set; }
            public string Sex { get; set; }
            public string Samedaydelivery { get; set; }
            public string OrderPrice { get; set; }
            public Addressinfo addressinfo { get; set; }
        }
    
        public class Addressinfo
        {
            public Maincust maincust { get; set; }
            public Billinginfo billinginfo { get; set; }
            public Shippinginfo shippinginfo { get; set; }
        }
    
        public class Maincust
        {
            public Maincustaddress[] maincustaddress { get; set; }
            public Maincontact[] Maincontact { get; set; }
        }
    
        public class Maincustaddress
        {
            public string Name { get; set; }
            public string Addressline1 { get; set; }
            public string Addressline2 { get; set; }
            public string city { get; set; }
            public string zipcode { get; set; }
        }
    
        public class Maincontact
        {
            public string firstname { get; set; }
            public string lastname { get; set; }
            public Contact[] contacts { get; set; }
        }
    
        public class Contact
        {
            public string type { get; set; }
            public string email { get; set; }
            public string number { get; set; }
        }
    
        public class Billinginfo
        {
            public Billaddress[] billaddress { get; set; }
            public Billingcontact[] billingcontact { get; set; }
        }
    
        public class Billaddress
        {
            public string Name { get; set; }
            public string Addressline1 { get; set; }
            public string Addressline2 { get; set; }
            public string city { get; set; }
            public string zipcode { get; set; }
        }
    
        public class Billingcontact
        {
            public string firstname { get; set; }
            public string lastname { get; set; }
            public Contact1[] contacts { get; set; }
        }
    
        public class Contact1
        {
            public string type { get; set; }
            public string email { get; set; }
            public string number { get; set; }
        }
    
        public class Shippinginfo
        {
            public Shippingaddress[] shippingaddress { get; set; }
            public Ship_Contact[] ship_contact { get; set; }
        }
    
        public class Shippingaddress
        {
            public string Name { get; set; }
            public string Addressline1 { get; set; }
            public string Addressline2 { get; set; }
            public string city { get; set; }
            public string zipcode { get; set; }
        }
    
        public class Ship_Contact
        {
            public string firstname { get; set; }
            public string lastname { get; set; }
            public Contact2[] contacts { get; set; }
        }
    
        public class Contact2
        {
            public string type { get; set; }
            public string email { get; set; }
            public string number { get; set; }
        }
    }

    Here is the output.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, December 7, 2016 4:27 AM
    Moderator
  • Hi Prahalnathan,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, December 13, 2016 11:16 AM
    Moderator
  • Hi,

    Sorry for the delay in response.

    I have the same class now. But my question is instead of having shippingaddress, billingaddress class separately can i have a class as address and while deserializing the same class should be used for both

    Thursday, December 29, 2016 5:02 AM
  • Hi Prahalnathan,

    Thank you for feedback.

    I used shippingaddress and billingaddress for example. I will put them is the a address class.

    Please try the following code. You could not put billingaddress and shippingaddress in one class. The Json file and classes are correspond to each other. It have different classes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace deserializeJsonFile2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string Json = @"{           
                                 'addressinfo': {                            
                                                  'billingaddress': [{
                                                                  'Name': 'XXX',
                                                                  'Addressline1': 'flatnumber',
                                                                  'Addressline2': 'Streetname',
                                                                  'city': 'London',
                                                                  'zipcode': '123123'}],
                                                   'shippingaddress': [{
                                                                  'Name': 'XXX',
                                                                  'Addressline1': 'flatnumber',
                                                                  'Addressline2': 'Streetname',
                                                                  'city': 'London',
                                                                  'zipcode': '123123'}]
                                          
                                                  }
                                }";
            }
        }
        public class Rootobject
        {
            public Addressinfo addressinfo { get; set; }
        }
    
        public class Addressinfo
        {
            public Billingaddress[] billaddress { get; set; }
            public Shippingaddress[] shippingaddress { get; set; }
        }
    
        public class Billingaddress
        {
            public string Name { get; set; }
            public string Addressline1 { get; set; }
            public string Addressline2 { get; set; }
            public string city { get; set; }
            public string zipcode { get; set; }
        }
    
        public class Shippingaddress
        {
            public string Name { get; set; }
            public string Addressline1 { get; set; }
            public string Addressline2 { get; set; }
            public string city { get; set; }
            public string zipcode { get; set; }
        }
    }


    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, December 29, 2016 8:49 AM
    Moderator