我从linq执行一个查询,它给了我一个列列表下面是查询:
var userList = context.Users.Where(x => x.UserFirstName.Contains(value.ToString()) && x.UserAccessLevel == (int)AuthUser.AuthUserUserType.Manager)
.Select(x =>new UserGridDetails(){ UserFirstName = x.UserFirstName, UserLastName = x.UserLastName, UserEmailAddress = x.UserEmailAddress})
.ToList();
output = JsonConvert.SerializeObject(userList);
这是来自查询的JSON数据
[{"UserFirstName":"Manager3","UserLastName":"ManagerLastName","UserEmailAddress":"manager3@test.com"}]
我试图将这些数据反序列化到一个c#类对象中,该对象具有以下类
public class UserJsonWrapper
{
public List<UserGridDetails> UserGridDetailses { get; set; }
}
public class UserGridDetails
{
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserEmailAddress { get; set; }
}
这是反序列化代码
UserJsonWrapper jsonToDataTable = JsonConvert.DeserializeObject<UserJsonWrapper>(value);
去掉包装器。你得到的是一个数组,而不是一个包含数组作为属性的对象。
var jsonToDataTable = JsonConvert.DeserializeObject<List<UserGridDetails>>(value);