Why can't I deserialize the response to my entity?

0

Environment: VS 2017 Entity Framework 6.0

Article Reference
https://dzone.com/articles/a-few-great-ways-to-consume-restful-apis-in-c

Question: How do I convert Web Api 2 call to my entity?

Person Entity

public partial class Person
{
    public int id { get; set; }
    public string name { get; set; }
}

...

Web API 2 Controller

namespace WebApplication2.Controllers
{
    public class PeopleController : ApiController
    {
        private testdbEntities db = new testdbEntities();

        // GET: api/People
        public IQueryable<Person> GetPeople()
        {
            return db.People;
        }

...

Can't get last line to work

var client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
var response = client.DownloadString("http://localhost:49777/api/People");
var releases = JArray.Parse(response);  //This works
Person p = JsonConvert.DeserializeObject<Person>(response); //How do I get this to work?

...

Error:

Newtonsoft.Json.JsonSerializationException HResult=0x80131500
Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WebApplication1.Person' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)

enter image description here

c#
asp.net
asked on Stack Overflow Mar 6, 2019 by Rod • edited Mar 21, 2019 by halfer

1 Answer

1

Do this instead..

var client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
var response = client.DownloadString("http://localhost:49777/api/People");
var releases = JArray.Parse(response);  //This works
var people = JsonConvert.DeserializeObject<Person[]>(response);

Changed from this

Person p = JsonConvert.DeserializeObject<Person>(response);

to this..

var people = JsonConvert.DeserializeObject<Person[]>(response);

In the article link that you included in question, they are using an endpoint that returns a list of releases. That is the reason they are parsing it to Json Array first.

Depending on what you are returning from your endpoint, your code will change. But since you said JArray.Parse( line is executing successfully, I assume your endpoint is also returning list of people. So you need to deserialize it to list of people rather than single people object.

answered on Stack Overflow Mar 6, 2019 by sateesh • edited Mar 6, 2019 by sateesh

User contributions licensed under CC BY-SA 3.0