0

I have an application where I am receiving a datafeed. The data looks like this after the code below is executed, the variable 'result' looks like this:

[
    { 
        "personName": "Avery Davis",
        "personOrganization": "01"
    },
    {
        "personName": "Chris Davis",
        "personOrganization": "01"
    },
    {
        "personName": "Tony Davis",
        "personOrganization": "01"
    },
    {
        "personName": "Cory Dirt",
        "personOrganization": "01"
    },
    {
        "personName": "Tyler Dirt",
        "personOrganization": "01"
    },
    {
        "personName": "Ann Ford",
        "personOrganization": "01"
    },
    {
        "personName": "Lauren Ford",
        "personOrganization": "01"
    },
    {
        "personName": "Lauren Ford",
        "personOrganization": "01"
    },
    {
        "personName": "Avery Franklin",
        "personOrganization": "01"
    }
]

I have a model class which looks like this:

public class AllPeople
{
    public List<PeopleList> data { get; set; }
}

public class PeopleList
{
    public string personName { get; set; }
    public string personOrganization { get; set; }
}

This is the code that I use to retrieve the data:

    private IEnumerable<PeopleList> GetPeople()
    {
        IEnumerable<PeopleList> peopleLists = null;

        var client = new WebClient();
        var data = client.DownloadData("https://localhost:44314/api/values");
        var stream = new MemoryStream(data);
        var obj = new DataContractJsonSerializer(typeof(string));
        var result = obj.ReadObject(stream).ToString();

        peopleLists = (IEnumerable<PeopleList>)JsonConvert.DeserializeObject<AllPeople>(result);            

        return peopleLists;
 }

I know the JsonConvert line is incorrect but I have been unsuccessful in figuring out how to convert the string into a list.

Any suggestions?

Thanks.

2
  • Please post a snippet of the json, including the json key for that array of people. This is very likely a model binding issue. Newtonsoft.json can handle lists without issue if the model binding is done correctly Commented Aug 23, 2022 at 15:40
  • JsonConvert.DeserializeObject<List<PeopleList>>(result) Commented Aug 23, 2022 at 15:42

1 Answer 1

2

In the JSON shown there's no property called data. So this isn't an instance of AllPeople. The JSON shown is just an array of PeopleList. (Which, incidentally, is a terrible name for a single instance of a "person".)

Deserialize it to a List<PeopleList>:

peopleLists = JsonConvert.DeserializeObject<List<PeopleList>>(result);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution. And I agree, with your critique.

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.