-2

I have 2 classes, User and UserResponse:

public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName{ get; set; }
        public decimal Balance { get; set; }
    }

public class UserResponse
    {
        public User UserInfo { get; set; }
        public string Age { get; set; }
        public string Gender { get; set; }
        public string Message { get; set; }
    }

And here is what I want to receive:

{
   "userInfo": {
      "id": "1",
      "firstName": "John",
      "lastName": "Doe",
      "balance": "1000"
    },
   "age": "25",
   "gender": "Male",
   "message": "Hello",
}

So the question is how can I convert the UserResponse class to the json I want?

bmo
  • 66
  • 10
  • What you need is Serialization. There are lots of tools and library that you can use and serialize your objects to json. Best options are System.Text.Json and Newtonsoft.Json – MrMoeinM Jun 24 '21 at 19:35
  • Yes, I know theoretically that I need serialization, but I do not know the syntax to build the method. – bmo Jun 24 '21 at 19:38

3 Answers3

0

You can use JavascripSerializer

using System.Web.Script.Serialization;
var json = new JavaScriptSerializer().Serialize(objUserResponse);
0

I believe you are new to this and want a working sample to kick off the work.

Here you go.

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
                    
public class Program
{
    public static void Main()
    {
        var o = new UserResponse();
        o.Age = "25";
        o.Gender = "Male";
        o.Message = "Hello";
        o.UserInfo = new User();
        o.UserInfo.Id = 1;
        o.UserInfo.FirstName = "John";
        o.UserInfo.LastName = "Doe";
        o.UserInfo.Balance = 1000M;
        var json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()}  );
        Console.WriteLine(json);
    }
}

public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName{ get; set; }
        public decimal Balance { get; set; }
    }

public class UserResponse
    {
        public User UserInfo { get; set; }
        public string Age { get; set; }
        public string Gender { get; set; }
        public string Message { get; set; }
    }

Try it out here...

Sample Code

Purna W
  • 141
  • 4
0

you can use JsonSerializer from System.Text.Json

using System;
using System.Text.Json;
                    
public class Program
{
    public static void Main()
    {
        UserResponse response = new UserResponse { Age = "35", Gender = "male", Message = "Hello", UserInfo = new User { Id = 1, Balance = 5.00m, FirstName = "John", LastName = "Smith" } };
        Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true}));
    }
}

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public decimal Balance { get; set; }
}

public class UserResponse
{
    public User UserInfo { get; set; }
    public string Age { get; set; }
    public string Gender { get; set; }
    public string Message { get; set; }
};

Result:

{
  "userInfo": {
    "id": 1,
    "firstName": "John",
    "lastName": "Smith",
    "balance": 5.00
  },
  "age": "35",
  "gender": "male",
  "message": "Hello"
}

Details you can find here:https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties