0

I am new in ASP.NET world and I am trying to achieve following.

I need to serialize API of this format

{
    "results": {
        "profile": {
            "firstname": "John,
            "lastname": "Newman",
        },
        "credit": {
             "amount": 30
        }
    }
}

The problem is that I don't know how to model my data. I need a results object, which contains 2 other objects (profile and credit). You can see some sample code below.

public class Results
{

    public class Data {
        public Profile profile { get; set; }
        public Credit credit {get; set; }
    }

    public class Profile {
        public String firstname {get; set; }
        public String lastname { get; set; }
    }

    public class Credit {
        public int amount { get; set; }
    }
}

static void Main(string[] args)
{

Results results= new Results
{
    Data = new Data{
        Profile = new Profile {
            firstname = "John",
            lastname = "Newman"
        },
        Credit = new Credit {
           balance = "30"
        }

    }

};

string json = JsonConvert.SerializeObject(results);

Console.WriteLine(json);

}

The error I get is "Member Data cannot be initialized. It is not a field or property. What am I doing wrong?

2 Answers 2

1

Try this instead:

    public class Profile
    {
        public String firstname { get; set; }
        public String lastname { get; set; }
    }

    public class Credit
    {
        public int amount { get; set; }
    }

    public class Result
    {
        public Profile profile { get; set; }
        public Credit credit { get; set; }

    }
    public class Wrapper
    {
        public Result results { get; set; }

    }

    static void Main(string[] args)
    {

        var wrapper = new Wrapper
        {
            results = new Result
            {
                profile = new Profile
                {
                    firstname = "John",
                    lastname = "Newman"
                },
                credit = new Credit
                    {
                        amount = 30
                    }
            }
        };

        string json = JsonConvert.SerializeObject(wrapper);

        Console.WriteLine(json);

    }
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

   static void Main(string[]args) {
    Base results = new Base() {
        Results = new Results() {               
            profile = new Profile() {
                firstname = "John",
                lastname = "Newman"
            },
            credit = new Credit() {
                amount = 30
            }               
        }
    };

    string json = JsonConvert.SerializeObject(results);

    Console.WriteLine(json);

    Console.ReadLine();
}

public class Base {
    public Results Results {get;set;}
}

public class Results{
    public Profile profile {get;set;}
    public Credit credit {get;set;}

}   

public class Profile{
    public String firstname {get;set;}
    public String lastname {get;set;}
}

public class Credit{
    public int amount {get;set;}
}

Class Base wraps Results class to get the required JSON structure.

1 Comment

This returned an extra data object, but I got the point:-) Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.