1

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have defined in my MVC 4 project the following routing: url: "{controller}/{action}/{id}", so I have to put the action in the url.
Yes, but you've also got defaults, so you could have two Index methods, Index(void) (get all persons), and Index(int id) (get single person).
I know, but I use that routing for this solution ;)
0

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}]

Comments

0

I changed code to and it worked out for me.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.