/// <summary> /// So we have some JSON. But what are we gonna do with that? What we really want our objects... like a Card.. a CardDeck... /// We can use a handy JSON Converter to turn our raw json into objects! /// You'll want to call these methods somewhere else - like in your Controller for example. /// Create in instance of THIS class (the DAL) in your controller. Then you'll be able to call the methods below there. /// In this demo, you'll see how these methods were called in the Index action. Why the index? Just for simplicity for demonstration. /// </summary> public CardDeckModel GetCardDeck() { string rawJson = GetCardDeckJson(); CardDeckModel cardDeck = JsonConvert.DeserializeObject <CardDeckModel>(rawJson); return(cardDeck); }
public CardDeckModel GetCardDeck() { // make request to api string url = @$"https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1"; HttpWebRequest request = WebRequest.CreateHttp(url); // store response from api HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // convert the http response into a string of raw json StreamReader reader = new StreamReader(response.GetResponseStream()); string JSON = reader.ReadToEnd(); // convert the raw json string into an object (CardDeckModel) and return it CardDeckModel cardDeck = JsonConvert.DeserializeObject<CardDeckModel>(JSON); return cardDeck; }