Razor JSON to list

Skythrust

Member
Jul 9, 2019
133
7
Hi all,

I was wondering if there are any possibilities to show all values from an JSON output in a list.

For now I am able to show just one value, but in the foreach I would like to show them all.
C#:
c#

JSON output/response is; [{"FrstName": "John","LastName": "Doe","MiddleName": null,},{"FrstName": "Jane","LastName": "Doe","MiddleName": null,},{"FrstName": "Hope","LastName": "Dyne","MiddleName": van,}]

dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);

Persons = new List<Person>();

Persons.Add(new Person()
{
    FrstName = jsonResponse.FrstName, // FrstName = jsonResponse[0].FrstName
    LastName = jsonResponse.LastName, // LastName = jsonResponse[0].LastName
    MiddleName = jsonResponse.MiddleName, // MiddleName = jsonResponse[0].MiddleName
});

with the [0] it works for just one record.
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Instead of deserializing into a dynamic, pass in a list of person and deserialize directly to your list.

var Persons = JsonConvert.DeserializeObject<List<Person>>(json);
 

Skythrust

Member
Jul 9, 2019
133
7
Instead of deserializing into a dynamic, pass in a list of person and deserialize directly to your list.

var Persons = JsonConvert.DeserializeObject<List<Person>>(json);
With using "FrstName = jsonResponse.FrstName" or something else?

Since when I try to add a new person to a list VS is giving me an error that FrstName does not contain a definition.
 
Last edited:

TheGeneral

Active Member
Dec 27, 2016
130
151
Using System.Text.Json (NOT Newtonsoft) you can do this quite easily:

C#:
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
    }

    public static class RazorCode
    {
        private static readonly JsonSerializerOptions Options = new()
        {
            PropertyNameCaseInsensitive = true
        };

        public static void Render()
        {
            var people = JsonSerializer.Deserialize<List<Person>>("string input", Options);
        }
    }
 

Skythrust

Member
Jul 9, 2019
133
7
I am a bit further in my project for now. Since I am able to read the string to a list. I also made a OrderBy option.

Now just 1 thing left over here and that is to realize pagination ToList();

On the internet I found this code below. but whatever I try I am able to see all the records or nothing.

C#:
int pagesize = 10;
int countitems = Persons.Count(); // Does have 17 records
int pagecount = countitems % pagesize <= 0 ? countitems / pagesize : (countitems / pagesize) + 1;
for (int page = 0; page < pagecount; page++)
{
    Persons = Persons.Skip(page * pagesize).Take(pagesize).ToList();
}

Next of this I would like to apply an autorefresh to pages. So after 10 seconds show me page 2 etc. etc.

Can anyone help me with this?
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Instead of +1 on the page size you should do Math.Ceiling so it gets to a full page number and you don't need to worry about rounding. You also wouldn't need the ternary operator. Just set page Count = Math.Ceiling(countitems / pagesize);

You're looping through the pages so it's going to print them all. You usually have some sort of indexer to know what page your on. Could be a URL parameter or component state variable. You only want to run the query once on your current page.
 

Users who are viewing this thread

Top