Renzzs
Renzzs

Reputation: 1076

JSON not deserialize correctly Web API

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?

Upvotes: 1

Views: 4400

Answers (3)

Renzzs
Renzzs

Reputation: 1076

I changed code to and it worked out for me.

var result = resp.Content.ReadAsAsync<Person>().Result;

Upvotes: 0

leon.io
leon.io

Reputation: 2824

The reason is that you're calling /api/person/GetPerson/1 (i.e. You're expecting a single result).

If you were calling /api/person/all - then you would use IEnumerable as you're expecting more than one result.

Also - might be worth removing the GetPerson part of the URI - it's REST, so api/person/id is enough information.

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

It would seem the error message is pretty explanatory. Whatever class you are trying to deserialize into is expecting an array. Your JSON is just an object. You can try placing square brackets around it to make it a single-item array.

Something like this:

[{"$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}]

Upvotes: 0

Related Questions