public List<OptionGroup> AddOptionGroups(JsonArray childArray) { List<OptionGroup> rootOptionGroups = new List<OptionGroup>(); string id = "", name = "", description = "", price = "", max_price = "-1", type = ""; string min_selection = "", max_selection = ""; int sel_dep = 0; foreach (JsonValue ch in childArray) { JsonObject child = ch.GetObject(); id = child["id"].GetString(); if (child["name"].ValueType.ToString() == "String") name = child["name"].GetString(); if (child["description"].ValueType.ToString() == "String") description = child["description"].GetString(); if (child["max_price"].ValueType.ToString() == "Number") max_price = child["max_price"].GetNumber().ToString(); type = child["type"].GetString(); if ( type == "option group") { if (child["min_selection"].ValueType.ToString() == "String") min_selection = child["min_selection"].GetString(); if (child["max_selection"].ValueType.ToString() == "String") max_selection = child["max_selection"].GetString(); if (child["sel_dep"].ValueType.ToString() == "Number") sel_dep = Convert.ToInt32(child["sel_dep"].GetNumber()); OptionGroup cOgroup = new OptionGroup(id, name, description, min_selection, max_selection, sel_dep, type); // recursively get the children if (child["children"].ValueType.ToString() == "Array") cOgroup.childOptions = AddOptions(child["children"].GetArray()); rootOptionGroups.Add(cOgroup); } } return rootOptionGroups; }
public async Task GetMenu() { // keep these consistent client_id = (App.Current as App).client_id; secret = (App.Current as App).secret; // Call an HTTP GET request. GET /merchant/{merchant_id}/hours string baseURL = "https://api.delivery.com/api/merchant/" + uniqueID // the id of the merchant + "/menu?client_id=" + client_id; // Receive the response and parse it (Json) var responseString = await client.GetStringAsync(baseURL); JsonObject jsObject = JsonObject.Parse(responseString); // Get the menu schedule...... // Check for warnings // Get the menu(s) int scheduleID = -1; JsonArray menusArray = jsObject["menu"].GetArray(); foreach (JsonValue m in menusArray) { JsonObject menuObject = m.GetObject(); string id_menu = "", name_Menu = "", description_Menu = "", ob_type = ""; id_menu = menuObject["id"].GetString(); name_Menu = menuObject["name"].GetString(); description_Menu = menuObject["description"].GetString(); ob_type = menuObject["type"].GetString(); // create the empty menu Menu newMenu = new Menu(id_menu, name_Menu, description_Menu, ob_type); ////////////////// Get the items of the menu JsonArray menuChildren = menuObject["children"].GetArray(); string id = "", name = "", description = "", min_qty = "", max_qty = "", price = "", max_price = "", itype = ""; foreach(JsonValue ii in menuChildren) { JsonObject itemObj = ii.GetObject(); id = itemObj["id"].GetString(); name = itemObj["name"].GetString(); if(itemObj["description"].ValueType.ToString() == "String") description = itemObj["description"].GetString(); // get the schedule id array if it exists for this item List<int> scheduleIDs = new List<int>(); if (itemObj.ContainsKey("schedule")) { JsonArray itemSchedule = itemObj["schedule"].GetArray(); foreach (JsonValue sVal in itemSchedule) { // get the id of the schedule that this item belongs to scheduleID = Convert.ToInt32(sVal.GetNumber()); scheduleIDs.Add(scheduleID); } } if (itemObj["min_qty"].ValueType.ToString() == "Number") min_qty = itemObj["min_qty"].GetNumber().ToString(); if (itemObj["max_qty"].ValueType.ToString() == "Number") max_qty = itemObj["max_qty"].GetNumber().ToString(); if (itemObj["price"].ValueType.ToString() == "Number") price = itemObj["price"].GetNumber().ToString(); if (itemObj["max_price"].ValueType.ToString() == "Number") max_price = itemObj["max_price"].GetNumber().ToString(); // get the item type itype = itemObj["type"].GetString(); Item newItem = new Item(id, name, description, min_qty, max_qty, price, max_price, itype); // attempt to get this item's children (images, price groups, option groups) List<OptionGroup> itemOptions = new List<OptionGroup>(); List<PriceGroup> itemPrices = new List<PriceGroup>(); List<Image> itemImages = new List<Image>(); if (itemObj["children"].ValueType.ToString() == "Array") { JsonArray itemChildren = itemObj["children"].GetArray(); // item's children foreach(JsonValue child in itemChildren) { JsonObject c_Object = child.GetObject(); string c_Type = "", c_name = "", c_description = "", c_ID = "", c_url = "", c_min_selection = "", c_max_selection = ""; int c_sel_dep = 0; c_Type = c_Object["type"].GetString(); c_ID = c_Object["id"].GetString(); if (c_Object["name"].ValueType.ToString() == "String") c_name = c_Object["name"].GetString(); if (c_Object["description"].ValueType.ToString() == "String") c_description = c_Object["description"].GetString(); // treat differently depending on what the object is if(c_Type == "image") { c_url = c_Object["url"].GetString(); Image newImage = new Image(c_ID, c_name, c_url, c_description, c_Type); itemImages.Add(newImage); } else if(c_Type == "price group") { if (c_Object["min_selection"].ValueType.ToString() == "Number") c_min_selection = c_Object["min_selection"].GetNumber().ToString(); if (c_Object["max_selection"].ValueType.ToString() == "Number") c_max_selection = c_Object["max_selection"].GetNumber().ToString(); PriceGroup newPriceGroup = new PriceGroup(c_ID, c_name, c_description, c_min_selection, c_max_selection, c_Type); // add the price group's children if (c_Object["children"].ValueType.ToString() == "Array") newPriceGroup.childOptions = AddOptions(c_Object["children"].GetArray()); itemPrices.Add(newPriceGroup); } else if(c_Type == "option group") { if (c_Object["min_selection"].ValueType.ToString() == "String") c_min_selection = c_Object["min_selection"].GetString(); if (c_Object["max_selection"].ValueType.ToString() == "String") c_max_selection = c_Object["max_selection"].GetString(); if (c_Object["sel_dep"].ValueType.ToString() == "Number") c_sel_dep = Convert.ToInt32(c_Object["sel_dep"].GetNumber()); OptionGroup newOptionGroup = new OptionGroup(c_ID, c_name, c_description, c_min_selection, c_max_selection, c_sel_dep, c_Type); // add the option group's children. call the AddOptions function in this case if (c_Object["children"].ValueType.ToString() == "Array") newOptionGroup.childOptions = AddOptions(c_Object["children"].GetArray()); itemOptions.Add(newOptionGroup); } } ////////////////////////////////////////////////////// newItem._optionGroup = itemOptions; newItem._priceGroup = itemPrices; newItem.Images = itemImages; } // add item to menu's children newMenu.items.Add(newItem); } // end item menus.Add(newMenu); } int i = 0; }