• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 57
  • Last Modified:

Make class to jSon string

My json string should look like this:
{
"plan":"test",
"name":"test",
"createuser": {
"username":"test",
"usermail":"test",
}
}

How does I build the user class so I can make a json string.
var user = new
{
      plan = "test",
      name= "test",
      createuser ???? {}
};
var jSon = JsonConvert.SerializeObject(user);
1
Jesper Christensen
Asked:
Jesper Christensen
  • 2
1 Solution
 
Nitin SontakkeDeveloperCommented:
May be, as follows, as I always create concrete classes:

public class User
{
  public String plan {get; set;}
  public String name {get; set;}
  public CreateUser createuser {get; set;}
}

public class CreateUser
{
  public String username {get; set;}
  public String usermail {get; set;}
}

Open in new window


Or

var user = new
{
  plan = "test",
  name= "test",
  createuser = new 
  {
    username = "test",
    usermail = "test"
  }
};

Open in new window

0
 
Jesper ChristensenProgrammerAuthor Commented:
Thank you! Which method would you perfer?

your first example, or your last?
0
 
Nitin SontakkeDeveloperCommented:
First, of course, as I already mentioned in my original comment. Frankly, never used second, so I don't even know if it works with deserialisation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now