Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my desired JSON format
JSON Structure

Following this structure I've created the Model. Please find the class

C#
public class Atlas_Purchase
    {
        public string ReferId { get; set; }
        public List<string> DestinationList { get; set; }
        public List<AtlasApplicantList> ApplicantList { get; set; }
        private List<AtlasCreditCardDetails> CreditCards = new List<AtlasCreditCardDetails>();
        public List<AtlasCreditCardDetails> CreditCard
        {
            get { return CreditCards; }
            set { CreditCards = value; }
        }

public class AtlasApplicantList
    {
      public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Dob { get; set; }
        public string Gender { get; set; }
}

public class AtlasCreditCardDetails
    {
        public string CardExpirationMonth { get; set; }
        public string CardExpirationYear { get; set; }
        public string CardHolderAddress1 { get; set; }
        private List<AtlasTransaction> Transactions = new List<AtlasTransaction>();
        public List<AtlasTransaction> Transaction
        {
            get { return Transactions; }
            set { Transactions = value; }
        }
    }


But When I serialize the data and I'm getting the JSON in this format
Incorrect JSON Structure

I have tried different ways to serialize the data but still no luck, please help to to resolve this. Thanks in advance.

Biswarup

What I have tried:

C#
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonData = js.Serialize(purchase);

String jsonData = JsonConvert.SerializeObject(purchase);
Posted
Updated 4-Apr-20 0:41am

1 solution

0) Don't post stuff on imgur that can easily be posted here.

1) You can't create json without data in your objects.

2) Your model isn't complete. There is no AtlasTransaction object defined in your example code

3) Why are you serializing it twice?

4) It looks like you have some sample json. Have you tried any of these to automagically create your models?

- Use the free json2csharp web tool without installing anything.

- If you have Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as class.

- Use the free jsonclassgenerator.exe

- The web tool app.quicktype.io (available in a link from json2csharp) also does not require installing anything.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900