3 Replies - 213 Views - Last Post: 31 August 2019 - 04:56 AM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

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

Posted 30 August 2019 - 09:52 PM

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 :

[HttpPost(Contracts.ApiRoutes.Login.PostUserLogin)]
        public  IActionResult 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 ?


Is This A Good Question/Topic? 0
  • +

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

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15356
  • View blog
  • Posts: 61,557
  • Joined: 12-June 08

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

Posted 31 August 2019 - 01:16 AM

I would suggest looking at newtonsofts json nugget package.

https://www.newtonsoft.com/json
Was This Post Helpful? 0
  • +
  • -

#3 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

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

Posted 31 August 2019 - 03:03 AM

OK i added newton soft library after that how to convert to json file as above
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7500
  • View blog
  • Posts: 15,541
  • Joined: 16-October 07

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

Posted 31 August 2019 - 04:56 AM

I mean, begin at the beginning: https://www.newtonso...alizingJSON.htm

e.g.
class Foo {
    public int Code { get; set; }
    public string Msg { get; set; }
}

class Result {
    public class UserDataType {
        public string Name { get; set; }
    }
    // for silly underscores
    [JsonProperty(PropertyName = "user_data")]
    public UserDataType UserData { get; set; }
    public Foo Foo { get; set; }
}


var result = new Result() {
    UserData = new Result.UserDataType() { Name = "Alice" },
    Foo = new Foo { Code = 911, Msg = "Help" }
};

Console.WriteLine(JsonConvert.SerializeObject(result));



Now, if you fundamentally don't care about the structure you're returning, and it doesn't look like you do, then you can build it dynamically:
dynamic user_data = new JObject();
user_data.name = "Alice";

dynamic foo = new JObject();
foo.code = 911;
foo.message = "Help";

dynamic result2 = new JObject();
result2.user_data = user_data;
result2.foo = foo;

Console.WriteLine(JsonConvert.SerializeObject(result2));
// or Console.WriteLine(result2.ToString());



Ref: https://www.newtonso...JsonDynamic.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1