/// <summary> /// Initializes a new instance of the <see cref="Receipe"/> class. /// </summary> /// <param name="jsonString">The json string.</param> public Receipe(String jsonString) { JsonObject jsonObject = JsonObject.Parse(jsonString); this.Id = (int)jsonObject.GetNamedNumber("Id"); this.Title = jsonObject.GetNamedString("Title"); this.Author = jsonObject.GetNamedString("Author"); this.PublicationDate = Convert.ToDateTime(jsonObject.GetNamedString("PublicationDate")); this.DishType = DishType.GetInstance(jsonObject.GetNamedObject("DishType").GetNamedString("Name")); this.Rating = new Rating(jsonObject.GetNamedObject("Rating").Stringify()); this.Difficulty = new Difficulty(jsonObject.GetNamedObject("Difficulty").Stringify()); this.Cost = new Cost(jsonObject.GetNamedObject("Cost").Stringify()); this.Vegetarian = jsonObject.GetNamedBoolean("Vegetarian"); this.HtmlReceipe = jsonObject.GetNamedString("HtmlReceipe"); try { if (jsonObject.GetNamedString("ToDoInstructions") != null) { this.ToDoInstructions = jsonObject.GetNamedString("ToDoInstructions"); } } catch { } this.WithAlcohol = jsonObject.GetNamedBoolean("WithAlcohol"); this.Image = jsonObject.GetNamedString("Image"); this.Description = jsonObject.GetNamedString("Description"); this.ingredients = new List <Ingredient>(); JsonArray ingredientsJson = jsonObject.GetNamedArray("Ingredients"); foreach (var ingredientObject in ingredientsJson) { Ingredient ingredient = new Ingredient(JsonObject.Parse(ingredientObject.Stringify())); ingredients.Add(ingredient); } }
/// <summary> /// Gets the receipe from json item. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> private Receipe getReceipeFromJSONItem(JsonObject item) { Receipe receipe = new Receipe(); foreach (var property in item) { if (property.Value.Stringify() != "null") { switch (property.Key) { case "author": receipe.Author = property.Value.GetString(); break; case "cost": receipe.Cost = new Cost((int)property.Value.GetNumber()); break; case "difficulty": receipe.Difficulty = new Difficulty((int)property.Value.GetNumber()); break; case "dishType": JsonObject dishType = property.Value.GetObject(); foreach (var dishTypeProperty in dishType) { if (dishTypeProperty.Key == "label") { receipe.DishType = DishType.GetInstance(dishTypeProperty.Value.GetString()); } } break; case "id": receipe.Id = (int)property.Value.GetNumber(); break; case "isVegetarian": receipe.Vegetarian = property.Value.GetBoolean(); break; case "published": receipe.PublicationDate = getDateTimeFromString(property.Value.GetString()); break; case "rating": receipe.Rating = new Rating((int)property.Value.GetNumber()); break; case "title": receipe.Title = property.Value.GetString(); break; case "withAlcohol": receipe.WithAlcohol = property.Value.GetBoolean(); break; case "pictures": var picturesArray = property.Value.GetArray(); string found = ""; if (picturesArray != null) { var pictureObject = picturesArray[1]; foreach (var picture in pictureObject.GetObject()) { if (picture.Key == "url") { found = picture.Value.GetString(); } } } receipe.Image = found; break; } } } return(receipe); }