private void downloadJsonCompleted(object sender, DownloadStringCompletedEventArgs e) { try { if (e.Error == null && !e.Cancelled) { string data = e.Result; JObject obj = JObject.Parse(data); movie.summary = JsonParsers.getValue(obj, "summary"); if (movie.genre == "" || movie.genre == null) { movie.genre = JsonParsers.getArray(obj, "genres"); } if (movie.title == "" || movie.title == null) { movie.title = JsonParsers.getValue(obj, "title"); } if (movie.year == "" || movie.year == null) { movie.year = JsonParsers.getValue(obj, "year"); } if (movie.rating == "" || movie.rating == null) { movie.rating = JsonParsers.getDouble(obj, "rating", "average"); } movie.star = Util.getStarPath(movie.rating); if (movie.rateNumber == "" || movie.rateNumber == null) { movie.rateNumber = JsonParsers.getValue(obj, "ratings_count"); } if (movie.posterUrl == "" || movie.posterUrl == null) { movie.posterUrl = JsonParsers.getDouble(obj, "images", "small"); } object[] countries = obj["countries"].ToArray(); if (movie.region == "" || movie.region == null) { movie.region = JsonParsers.getArray(obj, "countries"); } if (movie.posterUrl == "") { movie.posterUrl = App.imagePath + "default.png"; } setUI(); List <People> peoples = new List <People>(); JArray array = (JArray)obj["directors"]; for (int i = 0; i < array.Count; i++) { People people = new People(); people.posterUrl = JsonParsers.getDouble(array[i], "avatars", "small"); if (people.posterUrl == "") { people.posterUrl = App.imagePath + "default.png"; } people.id = JsonParsers.getValue(array[i], "id"); people.name = JsonParsers.getValue(array[i], "name"); people.positionName = "导演"; people.position = People.DIRECTOR; peoples.Add(people); } array = (JArray)obj["casts"]; for (int i = 0; i < array.Count; i++) { People people = new People(); people.posterUrl = JsonParsers.getDouble(array[i], "avatars", "small"); if (people.posterUrl == "") { people.posterUrl = App.imagePath + "default.png"; } people.id = JsonParsers.getValue(array[i], "id"); people.name = JsonParsers.getValue(array[i], "name"); people.positionName = ""; people.position = People.ACTOR; peoples.Add(people); } movie.peopleList = peoples; peopleList.ItemsSource = peoples; // Insert movie into cache Cache.insertMovie(movie); if (progressBar != null) { progressBar.Visibility = Visibility.Collapsed; } } else { var wEx = e.Error as WebException; if (wEx.Status == WebExceptionStatus.RequestCanceled) { if (App.isFromDormant) { App.isFromDormant = false; getMovieByID(); } } if (progressBar != null) { progressBar.Visibility = Visibility.Collapsed; } } } catch (WebException) { if (progressBar != null) { progressBar.Visibility = Visibility.Collapsed; } MessageBoxResult result = MessageBox.Show(AppResources.ConnectionError, "", MessageBoxButton.OK); } catch (Exception) { if (progressBar != null) { progressBar.Visibility = Visibility.Collapsed; } } }
/// <summary> /// Parse movie JSON /// </summary> /// <param name="movieJson">Movie JSON String</param> private Tuple <Movie, List <People> > parseMovieJson(String movieJson) { JObject obj = JObject.Parse(movieJson); movie.summary = JsonParsers.getValue(obj, "summary"); if (movie.genre == "" || movie.genre == null) { movie.genre = JsonParsers.getArray(obj, "genres"); } if (movie.title == "" || movie.title == null) { movie.title = JsonParsers.getValue(obj, "title"); } if (movie.year == "" || movie.year == null) { movie.year = JsonParsers.getValue(obj, "year"); } if (movie.rating == "" || movie.rating == null) { movie.rating = JsonParsers.getDouble(obj, "rating", "average"); } movie.star = Util.getStarPath(movie.rating); if (movie.rateNumber == "" || movie.rateNumber == null) { movie.rateNumber = JsonParsers.getValue(obj, "ratings_count"); } if (movie.posterUrl == "" || movie.posterUrl == null) { movie.posterUrl = JsonParsers.getDouble(obj, "images", "large"); } object[] countries = obj["countries"].ToArray(); if (movie.region == "" || movie.region == null) { movie.region = JsonParsers.getArray(obj, "countries"); } if (movie.posterUrl == "") { movie.posterUrl = App.imagePath + "default.png"; } List <People> peoples = new List <People>(); JArray array = (JArray)obj["directors"]; for (int i = 0; i < array.Count; i++) { People people = new People(); people.posterUrl = JsonParsers.getDouble(array[i], "avatars", "small"); if (people.posterUrl == "") { people.posterUrl = App.imagePath + "default.png"; } people.id = JsonParsers.getValue(array[i], "id"); people.name = JsonParsers.getValue(array[i], "name"); people.positionName = "导演"; people.position = People.DIRECTOR; peoples.Add(people); } array = (JArray)obj["casts"]; for (int i = 0; i < array.Count; i++) { People people = new People(); people.posterUrl = JsonParsers.getDouble(array[i], "avatars", "small"); if (people.posterUrl == "") { people.posterUrl = App.imagePath + "default.png"; } people.id = JsonParsers.getValue(array[i], "id"); people.name = JsonParsers.getValue(array[i], "name"); people.positionName = ""; people.position = People.ACTOR; peoples.Add(people); } return(Tuple.Create(movie, peoples)); }