public static List <Beers> GetStyleFromJSON(JObject mainObj) { List <Beers> beersStyleList = new List <Beers>(); var embedded = mainObj["_embedded"]["beer"]; int i = 0; try { while (embedded[i] != null) { try { JObject currentObj = (JObject)embedded[i]; Beers b = new Beers(); if (currentObj["Id"] != null) { b.Id = Convert.ToInt32(currentObj["Id"]); } if (currentObj["Name"] != null) { b.Name = currentObj["Name"].ToString(); } if (currentObj["BreweryId"] != null) { b.BreweryId = Convert.ToInt32(currentObj["BreweryId"]); } if (currentObj["BreweryName"] != null) { b.BreweryName = currentObj["BreweryName"].ToString(); } if (currentObj["StyleId"] != null) { b.StyleId = Convert.ToInt32(currentObj["StyleId"]); } if (currentObj["StyleName"] != null) { b.StyleName = currentObj["StyleName"].ToString(); } if (currentObj["_links"]["style"]["href"] != null) { b.StyleLink = currentObj["_links"]["style"]["href"].ToString(); } beersStyleList.Add(b); } catch { } i++; } } catch (Exception exp) { } return(beersStyleList); }
public static List <Beers> GetReviewBeerOrMoreInfoFromJSON(JObject mainObj) { List <Beers> beersStyleList = new List <Beers>(); try { if (mainObj != null) { JObject currentObj = (JObject)mainObj; Beers b = new Beers(); if (currentObj["Id"] != null) { b.Id = Convert.ToInt32(currentObj["Id"]); } if (currentObj["Name"] != null) { b.Name = currentObj["Name"].ToString(); } if (currentObj["BreweryId"] != null) { b.BreweryId = Convert.ToInt32(currentObj["BreweryId"]); } if (currentObj["BreweryName"] != null) { b.BreweryName = currentObj["BreweryName"].ToString(); } if (currentObj["StyleId"] != null) { b.StyleId = Convert.ToInt32(currentObj["StyleId"]); } if (currentObj["StyleName"] != null) { b.StyleName = currentObj["StyleName"].ToString(); } if (currentObj["_links"]["style"]["href"] != null) { b.StyleLink = currentObj["_links"]["style"]["href"].ToString(); } beersStyleList.Add(b); } } catch (Exception exp) { var a = exp.Message; } return(beersStyleList); }
public static void PostNewBeer(string url, string href) { string newUrl = url + href; Beers b = new Beers(); Console.WriteLine("Dati ID: "); b.Id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Dati Numele: "); b.Name = Console.ReadLine(); StringContent content = new StringContent(JsonConvert.SerializeObject(b)); var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json"); var clientResponse = client.PostAsync(newUrl, content).Result; Console.WriteLine(clientResponse); }
public static List <Beers> GetBeersDataFromJSON(JObject mainObj) { List <Beers> beersList = new List <Beers>(); try { JObject br = (JObject)mainObj["_embedded"]; var beers = br["beer"]; int i = 0; while (beers[i] != null) { try { JObject currentObj = (JObject)beers[i]; Beers b = new Beers(); if (currentObj["Id"] != null) { b.Id = Convert.ToInt32(currentObj["Id"]); } if (currentObj["Name"] != null) { b.Name = currentObj["Name"].ToString(); } if (currentObj["BreweryId"] != null) { b.BreweryId = Convert.ToInt32(currentObj["BreweryId"]); } if (currentObj["BreweryName"] != null) { b.BreweryName = currentObj["BreweryName"].ToString(); } if (currentObj["StyleId"] != null) { b.StyleId = Convert.ToInt32(currentObj["StyleId"]); } if (currentObj["StyleName"] != null) { b.StyleName = currentObj["StyleName"].ToString(); } if (currentObj["_links"]["style"]["href"] != null) { b.StyleLink = currentObj["_links"]["style"]["href"].ToString(); } if (currentObj["_links"]["self"]["href"] != null) { b.SelfLink = currentObj["_links"]["self"]["href"].ToString(); } try { // this is because not all have reviews attached if (currentObj["_links"]["review"]["href"] != null) { b.ReviewLink = currentObj["_links"]["review"]["href"].ToString(); } } catch { } beersList.Add(b); } catch { } i++; } } catch { } return(beersList); }