简体   繁体   中英

Cannot deserialize the current JSON object

Sup guys, been on this all day. can't figure out what i'm getting wrong. here's my class:

public class Staff
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public object PhoneNumber { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }
    public string JobTitle { get; set; }
    public string Manager { get; set; }

}

here's the code that calls the api:

 private List<Staff> getAllStaff()
    {
        try
        {
             var resultList = new List<Staff>();
             var client = new HttpClient();
            var data = client.GetAsync("http://localhost:8000/api/staff").ContinueWith(response =>
            {
                var result = response.Result;

                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                { 
                    var readResult = result.Content.ReadAsAsync<List<Staff>>();
                    readResult.Wait();
                    resultList = readResult.Result;

                }
            });


            data.Wait();

            return resultList;
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

The data being passed to the view:

public ActionResult Index()
    {
        var staff = getAllStaff();


        return View(staff);
    }

And finally a sample of the json returned:

 [ { "FirstName": "xxxx", "LastName": "xxxx", "PhoneNumber": "xxxx", "Email": "xxxx", "UserName": "xxxx", "JobTitle": "xxxx", "Manager": "xxxx" }, { "FirstName": "xxxx", "LastName": "xxxx", "PhoneNumber": "xxxx", "Email": "xxxx", "UserName": "xxxx", "JobTitle": "xxxx", "Manager": "xxxx" }, ] 

The error I keep getting:

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[json.Models.Staff]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

It is possible that the api is returning both arrays and single records. You code should cater for both if that is the case. You are also asking for trouble with those blocking calls. Make the code async all the way.

private async Task<List<Staff>> getAllStaff() {
    var resultList = new List<Staff>();
    var client = new HttpClient();
    var response = await client.GetAsync("http://localhost:8000/api/staff");
    if (response.StatusCode == System.Net.HttpStatusCode.OK) {
        bool arrayParseFailed = false;
        try {
            resultList = await response.Content.ReadAsAsync<List<Staff>>();
        } catch (Exception ex) {//Array pase failed so try single
            arrayParseFailed = true;
        }
        if (arrayParseFailed) {
            try {
                var staff = await response.Content.ReadAsAsync<Staff>();
                if (staff != null) {
                    resultList.Add(staff);
                }
            } catch (Exception ex) {
            }
        }
    }
    return resultList;
}

So now the action should also be updated to be async

public async Task<ActionResult> Index() {
    var staff = await getAllStaff();
    return View(staff);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM