【问题标题】:Cannot deserialize the current JSON array无法反序列化当前 JSON 数组
【发布时间】:2016-10-25 17:24:48
【问题描述】:

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

我创建了一个名为 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;
    }
}

这是找到时返回的内容。

这是实际错误。

更新 - 改为使用 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;
    }
}

【问题讨论】:

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


    【解决方案1】:

    您正在尝试将 JSON array 反序列化为 JSON object 并收到错误消息。为避免这种情况,只需更改此行

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

    进入这个

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

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

    【讨论】:

    • 谢谢...我将其更改为使用 List 并且看起来可行。我在上面发布了更新的代码。
    【解决方案2】:

    问题是您的控制器需要一个对象,但 API 正在传回一个包含单个条目的数组。

    您可以更改 API 以返回单个文档

    {
        "AppName": "AppAdmin",
        "Emp": "00762041",
        "Role": "APPADMIN"
    }
    

    或者您可以更改您的处理方式,使其处理一个数组并获取第一个元素。

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      相关资源
      最近更新 更多