1

Is there possible to display all element from "UserStatus" direct in json.

From: { "UserName": "test", "UserStatus" { "Id" : "1", "Status" "status" }}

To: { "UserName": "test", "Id" : "1", "Status" "status" }

public class UserStatus 
{
   public int Id { get; set; }
   public string Status { get; set; }
}
    
public class User 
{
   public string UserName { get; set; }
   public UserStatus Status { get; set; }
}

var user = new User();  

string json = JsonConvert.SerializeObject(user);
2
  • 1
    You can always create a flattened DTO and map properties to it. Commented Aug 9, 2021 at 15:11
  • You can use AutoMapper library for these kind of projections Commented Aug 10, 2021 at 8:45

1 Answer 1

1

try this

var user = new User() { UserName = "userName", Status = new UserStatus {Id=1,Status="status"}};
string json = JsonConvert.SerializeObject(new {
                                                  UserName=user.UserName,
                                                  Id=user.Status.Id,
                                                  Status=user.Status.Status
                                               });

output

{"UserName":"userName","Id":1,"Status":"status"}
Sign up to request clarification or add additional context in comments.

Comments

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.