如何检索对象中的值并将它们转换为不同的变量类型

本文关键字:转换 类型 变量 何检索 检索 对象 | 更新日期: 2023-09-27 18:09:18

我正在通过Json检索一些值,并将它们转换为对象。接下来,我想通过DBAccess.cs脚本将它们传递到数据库中。这是通过MVC完成的。我想要的只是获取存储在对象

中的值

controller.cs:

public IHttpActionResult PostRegister([FromBody] dynamic register)
{
    try
    {
        Newtonsoft.Json.Linq.JObject employeeRes = (Newtonsoft.Json.Linq.JObject)register.employeedetails;
        var employee = employeeRes.ToObject<Employee>();
        var inserted_id = uMgt.insertRegDetail(employeeRes);
        int nid = (int)inserted_id;
        return Ok("success");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return Ok("fail");
    }
}

userManagement.cs

public int insertRegDetail(Employee emp)
{
            try 
            {
                var new_id = db.PassRegistraiondetails("new_insert_detail", emp.Fname, emp.Lname, Convert.ToDateTime(emp.Date_of_Birth), emp.Nic, emp.Gender, emp.Email, emp.Mobile_no, Convert.ToInt32(emp.Department_name), emp.Designation, Convert.ToDateTime(emp.Date_of_join));
                return (new_id);
            }
            catch(Exception ex)
            {
              Console.WriteLine(ex.Message);
              return (0);
            }
}

最后是dbaccess。cs

public int PassRegistraiondetails(string spName, string FName, string LName, DateTime dob, string nic, string Gender, string email, int tel, int id, string Designation, DateTime doj)
{
            try
            {
                SqlCommand cmd = new SqlCommand();
                SqlParameter param;
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = spName;
                param = new SqlParameter("@fname", SqlDbType.NChar);
                param.Value = FName;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@lname", SqlDbType.NChar);
                param.Value = LName;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@DOB", SqlDbType.Date);
                param.Value = dob;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@gender", SqlDbType.NChar);
                param.Value = Gender;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@email", SqlDbType.NChar);
                param.Value = email;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@mobile_no", SqlDbType.Int);
                param.Value = tel;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@designation", SqlDbType.VarChar);
                param.Value = Designation;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@date_of_join", SqlDbType.Date);
                param.Value = doj;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@nic", SqlDbType.NChar);
                param.Value = nic;
                param.IsNullable = true;
                cmd.Parameters.Add(param);
                param = new SqlParameter("@dept_id", SqlDbType.Int);
                param.Value = id;
                param.IsNullable = false;
                cmd.Parameters.Add(param);
                cmd.Parameters.Add("@new_id", SqlDbType.Int).Direction = ParameterDirection.Output;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    this.OpenConnection();
                    cmd.ExecuteNonQuery();
                    int ID = Convert.ToInt32(cmd.Parameters["@new_id"].Value);
                    this.CloseConnection();
                    return ID;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return (0);
            }
}
我现在得到的错误是:

无法将Newtonsoft.Json.Linq.JObject转换为域名。员工

<——这里域是一个基类>

如何解决这个问题?我将不胜感激

这是我的JSON数据

{ "employeedetails": 
     {"Fname":"awad","Lname":"adadad","Date_of_Birth":"09/13/2016",
      "Nic":"asdasdasd","Gender":"Male",
      "Email":"asQsa@c","Mobile_no":"1234234234",
      "Designation":"asdasd","Date_of_join":"09/27/2016",
      "Department_name":"1"
     }
}

这是我的雇员。

namespace Efutures.HR.LeaveManagement.Domain
{
    public class Employee
    {
        public string Empid { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }
        public string Date_of_Birth { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }
        public int Mobile_no { get; set; }
        public string Designation { get; set; }
        public string Date_of_join { get; set; }
        public string Nic { get; set; }
        public string Department_name { get; set; }
    }
}

如何检索对象中的值并将它们转换为不同的变量类型

Ashane Alvis,如果您看到您正在获得的JSON,它有一个名为employeedetails的属性,其中包含员工信息的对象。

所以你需要一个类,假设Employee有一个名为employeedetails的属性,该属性的类型假设为EmployeeDetails,它在给定的JSON中具有主属性employeedetails中的其余属性。

检查下面的代码片段,它将把您提供的JSON转换为雇员对象。

namespace TestProject
{
    using Newtonsoft.Json;
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            var json = "{'"employeedetails'":{'"Fname'":'"awad'",'"Lname'":'"adadad'",'"Date_of_Birth'":'"09/13/2016'",'"Nic'":'"asdasdasd'",'"Gender'":'"Male'",'"Email'":'"asQsa @c'",'"Mobile_no'":'"1234234234'",'"Designation'":'"asdasd'",'"Date_of_join'":'"09/27/2016'",'"Department_name'":'"1'"}}";
            var employee = JsonConvert.DeserializeObject(json, typeof(Employee));
            Console.ReadKey();
        }
    }
    public class Employee
    {
        public EmployeeDetails employeedetails { get; set; }
    }
    public class EmployeeDetails
    {
        public string Empid { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }
        public string Date_of_Birth { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }
        public int Mobile_no { get; set; }
        public string Designation { get; set; }
        public string Date_of_join { get; set; }
        public string Nic { get; set; }
        public string Department_name { get; set; }
    }
}