c# - 无法反序列化当前的 JSON 数组

标签 c# asp.net asp.net-web-api asp.net-web-api2 dotnet-httpclient

我正在尝试从 MVC 项目调用 Web API 以检查员工是否有权访问该应用程序。我调用一个传递员工编号和应用程序名称的 Web API。如果他们有权访问,它将返回 EmployeeId、AppName 和 AppRoll。如果他们没有访问权限,它将发回一个空的 body 。

我创建了一个名为 EmployeeAccess 的模型,它具有 3 个属性以及调用 API 的方法。当我运行它时,出现错误“无法反序列化当前的 JSON 数组”。

在上面链接的示例中,他们没有对创建 JSON 对象做任何事情。任何人都知道我错过了什么或做错了什么?

HomeController (一些值是用于测试的硬编码)

public async System.Threading.Tasks.Task<ActionResult> Index()
{
    string employeeId = "762041";
    string appName = "AppAdmin";

    var objEmployeeAccess = new EmployeeAccess();
    EmployeeAccess employeeAccess = await objEmployeeAccess.GetEmployeeAccess(employeeId, appName);

    return View();
}

public class EmployeeAccess
{
    HttpClient client = new HttpClient();

    public string AppName { get; set; }
    public string Emp { get; set; }
    public string Role { get; set; }

    internal async Task<EmployeeAccess> GetEmployeeAccess(string employeeId, string appName)
    {
        string uri = ConfigurationManager.AppSettings["ApiUri"];

        string endPoint = string.Format("api/v1/GetAppRoleForEmp?EmpID={0}&AppName={1}", employeeId, AppName);

        client.BaseAddress = new Uri(uri);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        EmployeeAccess employee = null;
        HttpResponseMessage response = await client.GetAsync(endPoint);
        response.EnsureSuccessStatusCode();

        if (response.IsSuccessStatusCode)
        {
            employee = await response.Content.ReadAsAsync<EmployeeAccess>();
        }
        return employee;
    }
}

这是找到时返回的内容。 enter image description here

这是实际错误。 enter image description here

更新 - 更改为使用 List<> 使其工作

public async System.Threading.Tasks.Task<ActionResult> Index()
{
    string employeeId = "762041";
    string appName = "AppAdmin";

    var employeeAccess = new EmployeeAccess();
        List<EmployeeAccess> listEmployee = await employeeAccess.GetEmployeeAccess(employeeId, appName);

    return View();
}


public class EmployeeAccess
{
    HttpClient client = new HttpClient();

    public string AppName { get; set; }
    public string Emp { get; set; }
    public string Role { get; set; }

    internal async Task<EmployeeAccess> GetEmployeeAccess(string employeeId, string appName)
    {
        string uri = ConfigurationManager.AppSettings["ApiUri"];

        string endPoint = string.Format("api/v1/GetAppRoleForEmp?EmpID={0}&AppName={1}", employeeId, appName);

        client.BaseAddress = new Uri(uri);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await client.GetAsync(endPoint);
        response.EnsureSuccessStatusCode();

        List<EmployeeAccess> listEmployee = new List<EmployeeAccess>();
        if (response.IsSuccessStatusCode)
        {
            listEmployee = await response.Content.ReadAsAsync<List<EmployeeAccess>>();
        }

        return listEmployee;
    }
}

最佳答案

您正在尝试将 JSON 数组 反序列化为 JSON 对象,但出现错误。为避免这种情况,只需更改这一行

await response.Content.ReadAsAsync<EmployeeAccess>();

进入这个

await response.Content.ReadAsAsync<List<EmployeeAccess>>();

在你得到结果后取第一个元素并返回你的对象。

关于c# - 无法反序列化当前的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246053/

相关文章:

asp.net-mvc - 使用 MVC 和 WebAPI 避免 Resharper <location> web.config 警告

c# - 我应该使用 Path.GetRandomFileName 还是使用 Guid?

asp.net - 如何包含多个子对象?

c# - Visual Studio 代码(使用 C#): While putting debugger point then showing me "No Symbol loaded for this document"

c# - 从 session 到成员文件夹的身份验证

c# - 使用 Lambda 表达式从字段名称中选择不同的字段

asp.net - 401 向 web api 发送 ajax 请求时未经授权

c# - 在事件链接时消除顺序依赖性和冗余的干净方法是什么?

c# - MS Office COM 与多个 UAC 用户的互操作和线程安全

c# - Visual Studio Express 2013 中的代码契约(Contract)支持