Array with keys ASPNET/Razor

Skythrust

Member
Jul 9, 2019
133
7
Goodafternoon all!

I am trying to rebuild one of my application from PHP to ASP with Razor.
When I pick up the array plain from my Postman output and I paste this VS is not able to read this.

I have searched on Google to find a fix for this what I am doing wrong, but I can't really find a solution for this.

There a 2 lines, one of them is working (Members), the other one (MembersWithKeys) is not working.
Does anyone have an idea what's going wrong over here?

C#:
@page
@model IndexModel

@{
    string[] Members = {"John", "Jane", "Jhon"};
    string[] MembersWithKey = [{ "Firstname":"John","Lastname":"Doe","Age":"37"},{ "Firstname":"Jane","Lastname":"Doe","Age":"27"},{ "Firstname":"Jhon","Lastname":"Doe","Age":"41"}];
}

@foreach (var Person in Members)
{
    <button>@Person</button>
}
 

TheGeneral

Active Member
Dec 27, 2016
134
152
You need to deserialize it from Json

Code:
var members = JsonSerializer.Deserialize<Person[]>("Your JSON string here", new JsonSerializerOptions() { PropertyNameCaseInsensitive = true })

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}
 

Users who are viewing this thread

Top