简体   繁体   中英

I want to serialize JSON from array to object

This is my desired JSON format

{ "ReferId": "9800", 
"DestinationList":["250"], 
"EmailAddress": "Test_Emails@gmail.com", 
"ApplicantList":[ 
{
"FirstName":"Test", 
"LastName":"Test", 
"Dob":"01/01/1990", 
"Gender":"M", 
} ], 
"CreditCard": { 
"CardExpirationMonth": 1, 
"CardExpirationYear": 2021, 
"CardHolderAddress1": "123 Main", 
"Transaction": {"Amount":106.56} 
} 
} 

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

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

不正确的 JSON 结构

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

Biswarup

public class AtlasTransaction { public decimal Amount {get;set;} }

Do you have this class? Also instead of this code

private List<AtlasCreditCardDetails> CreditCards = new List<AtlasCreditCardDetails>();
public List<AtlasCreditCardDetails> CreditCard
{
get { return CreditCards; }
set { CreditCards = value; }
}

write this much only public List<AtlasCreditCardDetails> CreditCard {get;set;}

same way change in class AtlasCreditCardDetails for property Transaction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM