Map a Json to a Viewmodel via Automapper

Asked

Viewed 49 times

0

I own the following Json (I’m betting just a piece of it).

{
"filters": {},
"competition": {
    "id": 2021,
    "area": {
        "id": 2072,
        "name": "England"
    },
    "name": "Premier League",
    "code": "PL",
    "plan": "TIER_ONE",
    "lastUpdated": "2019-05-12T20:05:45Z"
},
"season": {
    "id": 468,
    "startDate": "2019-08-10",
    "endDate": "2020-05-17",
    "currentMatchday": 1,
    "winner": null
},
"standings": [
    {
        "stage": "REGULAR_SEASON",
        "type": "TOTAL",
        "group": null,
        "table": [
            {
                "position": 1,
                "team": {
                    "id": 76,
                    "name": "Wolverhampton Wanderers FC",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/en/f/fc/Wolverhampton_Wanderers.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 2,
                "team": {
                    "id": 58,
                    "name": "Aston Villa FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/9/9f/Aston_Villa_logo.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 3,
                "team": {
                    "id": 64,
                    "name": "Liverpool FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/0/0a/FC_Liverpool.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 4,
                "team": {
                    "id": 65,
                    "name": "Manchester City FC",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/en/e/eb/Manchester_City_FC_badge.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 5,
                "team": {
                    "id": 67,
                    "name": "Newcastle United FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/5/56/Newcastle_United_Logo.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 6,
                "team": {
                    "id": 57,
                    "name": "Arsenal FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/en/5/53/Arsenal_FC.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 7,
                "team": {
                    "id": 61,
                    "name": "Chelsea FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/5/5c/Chelsea_crest.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 8,
                "team": {
                    "id": 73,
                    "name": "Tottenham Hotspur FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/b/b4/Tottenham_Hotspur.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 9,
                "team": {
                    "id": 62,
                    "name": "Everton FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/f/f9/Everton_FC.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 10,
                "team": {
                    "id": 66,
                    "name": "Manchester United FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 11,
                "team": {
                    "id": 68,
                    "name": "Norwich City FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/8/8c/Norwich_City.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 12,
                "team": {
                    "id": 356,
                    "name": "Sheffield United FC",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/en/9/9c/Sheffield_United_FC_logo.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 13,
                "team": {
                    "id": 338,
                    "name": "Leicester City FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/en/6/63/Leicester02.png"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 14,
                "team": {
                    "id": 328,
                    "name": "Burnley FC",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/en/0/02/Burnley_FC_badge.png"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 15,
                "team": {
                    "id": 563,
                    "name": "West Ham United FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/e/e0/West_Ham_United_FC.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 16,
                "team": {
                    "id": 340,
                    "name": "Southampton FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/c/c9/FC_Southampton.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 17,
                "team": {
                    "id": 354,
                    "name": "Crystal Palace FC",
                    "crestUrl": "http://upload.wikimedia.org/wikipedia/de/b/bf/Crystal_Palace_F.C._logo_%282013%29.png"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 18,
                "team": {
                    "id": 397,
                    "name": "Brighton & Hove Albion FC",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/en/f/fd/Brighton_%26_Hove_Albion_logo.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 19,
                "team": {
                    "id": 346,
                    "name": "Watford FC",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/en/e/e2/Watford.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "position": 20,
                "team": {
                    "id": 1044,
                    "name": "AFC Bournemouth",
                    "crestUrl": "https://upload.wikimedia.org/wikipedia/de/4/41/Afc_bournemouth.svg"
                },
                "playedGames": 0,
                "won": 0,
                "draw": 0,
                "lost": 0,
                "points": 0,
                "goalsFor": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            }
        ]
    }
]
}

I’d like to map the standings to my viewModel:

public class CompetitionViewModel
{
    [Display(Name = "Posição")]
    public int Position { get; set; }

    [Display(Name = "Nome")]
    public string Name { get; set; }

    [Display(Name = "Vitórias")]
    public int Won { get; set; }

    [Display(Name = "Empates")]
    public int Draw { get; set; }

    [Display(Name = "Derrotas")]
    public int Lost { get; set; }

    [Display(Name = "Pontos")]
    public int Points { get; set; }
}

And here I deserialized the Object:

public class ApplicationCompetition
{
    public Competition Competition { get; set; }

    public Season Season { get; set; }

    public List<Standings> Standings { get; set; }
}

public class Competition
{
    public int Id { get; set; }

    public Area Area { get; set; }

    public string Name { get; set; }

    public string Code { get; set; }

    public string Plan { get; set; }

    public DateTime LastUpdated { get; set; }
}

public class Area
{
    public int Id { get; set; }

    public string Name { get; set; }
}

public class Season
{
    public int Id { get; set; }

    public DateTime StartDate { get; set; }

    public DateTime EndDate { get; set; }

    public int CurrentMatchday { get; set; }

    public string Winner { get; set; }
}

public class Standings
{
    public string Stage { get; set; }

    public string Type { get; set; }

    public string Group { get; set; }

    public List<Table> Table { get; set; }
}

public class Table
{
    public int Position { get; set; }

    public Team Team { get; set; }

    public int PlayedGames { get; set; }

    public int Won { get; set; }

    public int Draw { get; set; }

    public int Lost { get; set; }

    public int Points { get; set; }

    public int GoalsFor { get; set; }

    public int GoalsAgainst { get; set; }

    public int GoalDifference { get; set; }
}

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }

    public string CrestUrl { get; set; }
}

The part about disallowing the object is all right, but my problem is when mapping this disallowed object to my viewModel:

_mapper.Map<IEnumerable<CompetitionViewModel>>(JsonConvert.DeserializeObject<ApplicationCompetition>(response.Content.ReadAsStringAsync().Result));

This is the part where I’m beating myself up to be able to map the object, I don’t know how to map the list of standings, catching the name that is within the team:

public MappingProfile()
{
    CreateMap<ApplicationCompetition, CompetitionViewModel>()
            .ForMember(x => x.Position, y => y.MapFrom(z => z.Standings[0].Table[0].Position));
 }

For example, I need to map the standings => team => name, for the attribute Name, in my Viewmodel.

This is my Object, I need to create the Viewmodel, just with the data inside the Table:

inserir a descrição da imagem aqui

  • 1

    given that standings and table are lists, because it would result in only one name and not n names?

  • I suggest copying json and pasting json special as a class in visual studio. so it will be the exact class you need, to serialize and deserialize.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.