none
How to convert User class object to json file when success login ? RRS feed

  • Question

  • problem

    How to convert User class object to json file when success login ?

    I need to convert user class object to json file represent status of login and data related as following ;

    this is actually desired result 

    {
    "request_status" : {
    "status" : "succeeded",
    "code": 0,
    "message" : "login succeeded!"
    },
    "user_data": { 
      "UserFullName" : "Admin", 
      "LoginTime" : "12:00"
      
    },
    "branches": [
    { 
      "BranchCode" : "1",
      "BranchName":"Baha" 
      
    }
    ]
    }
    

    What I try is :

            public  JsonResult PostUserLogins(Users user)
            {
                int LoginStatus = _AuthunticateService.PostUserLogin(user.UserName,user.Password ,out DataTable Branches,out string errorMessage, out int statusCode);
                if(LoginStatus == 0)
                {
                    user.StatusCode = statusCode;
                    user.StatusText = "succeeded";
                    user.MessageStatus = "login succeeded!";
                    if (Branches.Rows.Count > 0)
                    {
                        user.dtgetBranches = Branches;
                    }
                    user.LoginTime = DateTime.Now.ToString();
    // How to convert user class after assign values to json result as script above diplay
                }
             }
    public class Users
    {
    public string UserName { get; set; }
    public string Password { get; set; }
    public string MessageStatus { get; set; }
    public int StatusCode { get; set; }
    public string StatusText { get; set; }
    public string IpAddress { get; set; }
    public string BrowserInfo { get; set; }
    public DataTable dtgetBranches { get; set; }
    public string LoginTime { get; set; }
    }

    How to convert user class object to json format as above ?


    Saturday, August 31, 2019 4:10 AM

All replies

  • Hello,

    I recommend not using a DataTable but instead use a class for Branches then use the following code.

    public class Users
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string MessageStatus { get; set; }
        public int StatusCode { get; set; }
        public string StatusText { get; set; }
        public string IpAddress { get; set; }
        public string BrowserInfo { get; set; }
        public List<Branch> Branches { get; set; }
        public string LoginTime { get; set; }
    }
    
    public class Branch
    {
        public int BranchCode { get; set; }
        public string BranchName { get; set; }
    }

    Demo

    var userItem = new Users()
    {
        UserName = "Jim",
        StatusCode = 1,
        Branches = new List<Branch>()
        {
            new Branch() { BranchCode = 12, BranchName = "AAA"},
            new Branch() { BranchCode = 33, BranchName = "DFG"}
        }
    };
    var userItemAsJson = new JavaScriptSerializer().Serialize(userItem);

    There is also Newtonsoft.Json from NuGet

    var userItem = new Users()
    {
        UserName = "Jim",
        StatusCode = 1,
        Branches = new List<Branch>()
        {
            new Branch() { BranchCode = 12, BranchName = "AAA"},
            new Branch() { BranchCode = 33, BranchName = "DFG"}
        }
    };
    
    var userItemAsJson = Newtonsoft.Json
        .JsonConvert.SerializeObject(userItem);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, August 31, 2019 9:55 AM
    Moderator
  • thank you for reply

    list <branches > branches = dtgetbranches ;

    how to fill list of branches by datatable have data as 

    BranchId,BranchName

    Saturday, August 31, 2019 6:59 PM
  • thank you for reply

    list <branches > branches = dtgetbranches ;

    how to fill list of branches by datatable have data as 

    BranchId,BranchName

    I was inferring not to use a DataTable, instead use a list. To convert a DataTable to a list see the following article with code samples.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 31, 2019 11:02 PM
    Moderator
  • Hello,

    Checking in to see how you are doing with this issue, has it been resolved or do you need more assistance?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 4, 2019 10:04 AM
    Moderator