I’m getting a error when converting JSON to an object. I can get the JSON thru Fiddler, so there is nothing wrong with my Web API I guess.
This is the error that I have:
{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[MvcMobile.WebAPI.Model.Person]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. 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.\r\nPath 'BusinessEntityID', line 1, position 30."}
This is the JSON:
{"$id":"1", "BusinessEntityID":1,"PersonType":"EM","NameStyle":false,"Title":null,
"FirstName":"Ken", "MiddleName":"J","LastName":"Sánchez","Suffix":null,
"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"0",
"rowguid":"92c4279f-1207-48a3-8448-4636514eb7e2",
"ModifiedDate":"2003-02- 08T00:00:00","Employee":null}
And this is the code:
HttpResponseMessage resp = client.GetAsync("http://localhost:8080/api/person/GetPerson/" + id).Result;
resp.EnsureSuccessStatusCode();
var result = resp.Content.ReadAsAsync<IEnumerable<Person>>().Result;
In Entity Framework I have set the following two options:
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
Could it be because I only get back one entity that JSON has a problem with that? Or something with missing []{}? Or mabye their is a better way to go from JSON to an object?