C#C
C#3y ago
30 replies
Jochem

❔ HTTP request object creation / ignoring fields when deserializing JSON

Hey there. I am writing a program using the Deck of Cards API which can be found here: https://deckofcardsapi.com/. The API returns a response which contains JSON.
class Deck 
{
  public Deck() { ... }
  public Card DrawCard() { ... }
}

I have a class named "Deck", I first wanted "Deck" to load and deserialize itself in the constructor using the API but I don't really know if that's possible or a good idea. I am wondering if there are better alternatives or best practices for this problem.

class Card
{
  public string Value;
  public string Suit;
  public string Image;
} 

I also have a second problem. Whenever I want to request a card using the API, the response contains many properties I don't need. Is there a way of only deserializing certain properties? Above you can see a class called "Card" and I only need "Value", "Suit", and "Image" but the JSON in the response contains much more as you can see below.

Response when drawing card(s).
{
    "success": true, 
    "deck_id": "kxozasf3edqu", 
    "cards": [
        {
            "code": "6H", 
            "image": "https://deckofcardsapi.com/static/img/6H.png", 
            "images": {
                          "svg": "https://deckofcardsapi.com/static/img/6H.svg", 
                          "png": "https://deckofcardsapi.com/static/img/6H.png"
                      }, 
            "value": "6", 
            "suit": "HEARTS"
        }, 
        {
            "code": "5S", 
            "image": "https://deckofcardsapi.com/static/img/5S.png", 
            "images": {
                          "svg": "https://deckofcardsapi.com/static/img/5S.svg", 
                          "png": "https://deckofcardsapi.com/static/img/5S.png"
                      }, 
            "value": "5", 
            "suit": "SPADES"
        }
    ], 
    "remaining": 50
}
Deck of Cards - an API for playing cards.
Deck of Cards
Was this page helpful?