前端js传json对象,c#后端接收
工具类部分
public T ObjectToEntity(object asObject) where T : new()
{
//创建实体对象实例
var t = Activator.CreateInstance();
// user userModel = new UserOperation().ConvertObject(obj);
IDictionary iDictionary = ((IDictionary)asObject);
//查找实体类属性是否和object属性相同
foreach (var info in typeof(T).GetProperties())
{
if (iDictionary.ContainsKey(info.Name))
{
//赋值给实体类
info.SetValue(t, iDictionary[info.Name], null);
}
}
return t;
}
调用工具类,注意:使用工具必须创建实体类
user ss=ObjectToEntity(json);
实体类部分
public class user
{
public user()
{
}
//用户名
public string userName;
//密码
public string passWord;
public string UserName { get => userName; set => userName = value; }
public string PassWord { get => passWord; set => passWord = value; }
}