public static Recipe FromJson(Context context, JSONObject json) { var recipe = new Recipe (); try { recipe.TitleText = json.GetString(Constants.RecipeFieldTitle); recipe.SummaryText = json.GetString(Constants.RecipeFieldSummary); if (json.Has(Constants.RecipeFieldImage)) { recipe.RecipeImage = json.GetString(Constants.RecipeFieldImage); } JSONArray ingredients = json.GetJSONArray(Constants.RecipeFieldIngredients); recipe.IngredientsText = ""; for (int i = 0; i < ingredients.Length(); i++) { recipe.IngredientsText += " - " + ingredients.GetJSONObject(i).GetString(Constants.RecipeFieldText) + "\n"; } JSONArray steps = json.GetJSONArray(Constants.RecipeFieldSteps); for (int i = 0; i < steps.Length(); i++) { var step = steps.GetJSONObject(i); var recipeStep = new RecipeStep(); recipeStep.StepText = step.GetString(Constants.RecipeFieldText); if (step.Has(Constants.RecipeFieldName)) { recipeStep.StepImage = step.GetString(Constants.RecipeFieldImage); } recipe.RecipeSteps.Add(recipeStep); } } catch (Exception ex) { Log.Error (Tag, "Error loading recipe: " + ex); return null; } return recipe; }
private void getMoviePosterPaths (string popularMovies, GridView view){ var pathsJson = new JSONObject(popularMovies); var pathArray = pathsJson.GetJSONArray("results"); paths.Clear(); for (var i = 0; i < pathArray.Length(); i++) { var posterPath = pathArray.GetJSONObject(i); paths.Add(posterPath.GetString("poster_path")); movieIds.Add(posterPath.GetInt("id")); } var imgAdapter = new ImageAdapter(Activity, paths, movieIds); view.Adapter = imgAdapter; }
private void getCategories(Action<List<LevelCategory>> callback) { List<LevelCategory> categories = new List<LevelCategory>(); var client = new RestClient("http://webmat.cs.aau.dk/api/"); var request = new RestRequest(Method.POST); request.AddParameter("action", "get_levels"); request.AddParameter("token", token); string data; try { data = client.Execute(request).Content; } catch (TimeoutException) { getCategories(callback); return; } JSONObject json = new JSONObject(data); JSONArray jsonArray = json.GetJSONArray("data"); for (int index = 0; index < jsonArray.Length(); index++) { LevelCategory levelCategory = new LevelCategory(jsonArray.GetJSONObject(index).GetString("name")); var levelsData = jsonArray.GetJSONObject(index).GetJSONArray("levels"); for (int i = 0; i < levelsData.Length(); i++) { List<string> starExpressions = new List<string>(); var starArray = levelsData.GetJSONObject(i).GetJSONArray("star_expressions"); for (int y = 0; y < starArray.Length(); y++) { starExpressions.Add(starArray.GetString(y)); } Level level = new Level( levelsData.GetJSONObject(i).GetInt("id"), levelsData.GetJSONObject(i).GetString("initial_expression"), levelsData.GetJSONObject(i).GetInt("stars"), levelsData.GetJSONObject(i).GetString("current_expression"), starExpressions.ToArray()); levelCategory.Add(level); } categories.Add(levelCategory); } callback(categories); }
List<Item> ParseJson(JSONObject json) { List<Item> result = new List<Item> (); try { JSONArray items = json.GetJSONArray(Constants.RecipeFieldList); for (int i = 0; i < items.Length(); i ++) { JSONObject item = items.GetJSONObject(i); Item parsed = new Item(); parsed.Name = item.GetString(Constants.RecipeFieldName); parsed.Title = item.GetString(Constants.RecipeFieldTitle); if (item.Has(Constants.RecipeFieldImage)) { String imageFile = item.GetString(Constants.RecipeFieldImage); parsed.Image = AssetUtils.LoadBitmapAsset(context, imageFile); } parsed.Summary = item.GetString(Constants.RecipeFieldSummary); result.Add(parsed); } } catch (Exception ex) { Log.Error (Tag, "Failed to parse recipe list: " + ex); } return result; }
public static Question FromJSon (JSONObject questionObject, int questionIndex) { try { string question = questionObject.GetString (JsonUtils.JSON_FIELD_QUESTION); JSONArray answersJsonArray = questionObject.GetJSONArray (JsonUtils.JSON_FIELD_ANSWERS); string[] answers = new string[JsonUtils.NUM_ANSWER_CHOICES]; for (int j = 0; j < answersJsonArray.Length (); j++) { answers [j] = answersJsonArray.GetString (j); } int correctIndex = questionObject.GetInt (JsonUtils.JSON_FIELD_CORRECT_INDEX); return new Question (question, questionIndex, answers, correctIndex); } catch (JSONException) { return null; } }
public void GetPlayers(Action<List<Player>> callback) { List<Player> players = new List<Player>(); var client = new RestClient("http://webmat.cs.aau.dk/api/"); var request = new RestRequest(Method.POST); request.AddParameter("action", "get_users"); request.AddParameter("token", token); string data; try { data = client.Execute(request).Content; } catch (TimeoutException) { GetPlayers(callback); return; } JSONObject json = new JSONObject(data); JSONArray jsonArray = json.GetJSONArray("data"); for (int index = 0; index < jsonArray.Length(); index++) { var jsonPlayer = jsonArray.GetJSONObject(index); Player player = new Player(jsonPlayer.GetString("name")); var badges = jsonPlayer.GetJSONArray("badges"); for (int badgeIndex = 0; badgeIndex < badges.Length(); badgeIndex++) { string badgeString = badges.GetString(badgeIndex); if (badgeString != "") { player.Badges.Add((BadgeName)int.Parse(badgeString)); } } players.Add(player); } callback(players); }
private void getWeatherDataFromJson(String forecastJsonStr, String locationSetting) { // These are the names of the JSON objects that need to be extracted. // Location information const string OWM_CITY = "city"; const string OWM_CITY_NAME = "name"; const string OWM_COORD = "coord"; const string OWM_LATITUDE = "lat"; const string OWM_LONGITUDE = "lon"; const string OWM_LIST = "list"; const string OWM_PRESSURE = "pressure"; const string OWM_HUMIDITY = "humidity"; const string OWM_WINDSPEED = "speed"; const string OWM_WIND_DIRECTION = "deg"; const string OWM_TEMPERATURE = "temp"; const string OWM_MAX = "max"; const string OWM_MIN = "min"; const string OWM_WEATHER = "weather"; const string OWM_DESCRIPTION = "main"; const string OWM_WEATHER_id = "id"; try { JSONObject forecastJson = new JSONObject (forecastJsonStr); JSONArray weatherArray = forecastJson.GetJSONArray (OWM_LIST); JSONObject cityJson = forecastJson.GetJSONObject (OWM_CITY); String cityName = cityJson.GetString (OWM_CITY_NAME); JSONObject cityCoord = cityJson.GetJSONObject (OWM_COORD); double cityLatitude = cityCoord.GetDouble (OWM_LATITUDE); double cityLongitude = cityCoord.GetDouble (OWM_LONGITUDE); long locationId = AddLocation (locationSetting, cityName, cityLatitude, cityLongitude); ArrayList jsonResultValues = new ArrayList (); DateTime dayTime = DateTime.UtcNow; for (int i = 0; i < weatherArray.Length (); i++) { // These are the values that will be collected. long dateTime; double pressure; int humidity; double windSpeed; double windDirection; double high; double low; string description; int weatherId; // Get the JSON object representing the day JSONObject dayForecast = weatherArray.GetJSONObject (i); // The date/time is returned as a long. We need to convert that // into something human-readable, since most people won't read "1400356800" as // "this saturday". // Cheating to convert this to UTC time, which is what we want anyhow dateTime = dayTime.AddDays (i).Ticks; pressure = dayForecast.GetDouble (OWM_PRESSURE); humidity = dayForecast.GetInt (OWM_HUMIDITY); windSpeed = dayForecast.GetDouble (OWM_WINDSPEED); windDirection = dayForecast.GetDouble (OWM_WIND_DIRECTION); // Description is in a child array called "weather", which is 1 element long. // That element also contains a weather code. JSONObject weatherObject = dayForecast.GetJSONArray (OWM_WEATHER).GetJSONObject (0); description = weatherObject.GetString (OWM_DESCRIPTION); weatherId = weatherObject.GetInt (OWM_WEATHER_id); // Temperatures are in a child object called "temp". Try not to name variables // "temp" when working with temperature. It confuses everybody. JSONObject temperatureObject = dayForecast.GetJSONObject (OWM_TEMPERATURE); high = temperatureObject.GetDouble (OWM_MAX); low = temperatureObject.GetDouble (OWM_MIN); ContentValues weatherValues = new ContentValues (); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_LOC_KEY, locationId); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_DATE, dateTime); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_HUMIDITY, humidity); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_PRESSURE, pressure); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_WIND_SPEED, windSpeed); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_DEGREES, windDirection); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_MAX_TEMP, high); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_MIN_TEMP, low); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_SHORT_DESC, description); weatherValues.Put (WeatherContractOpen.WeatherEntryOpen.COLUMN_WEATHER_id, weatherId); jsonResultValues.Add (weatherValues); } BulkInsertWeather (jsonResultValues, locationSetting); } catch (Exception ex) { Log.Error ("Featch Weather Task", ex.Message); } }