コード例 #1
0
        /// <summary>
        /// Downloads the JSON from the given URI and parses this into a dictionary
        /// </summary>
        /// <param name="url">The URI to download</param>
        /// <param name="movieInfo">The reference object to store the resulting MovieInfo object in</param>
        /// <returns>The result status code for this service call</returns>
        ServiceResultStatus LoadMovieData(string url, out MovieInfo movieInfo)
        {
            movieInfo = null;

            // Download JSON
            var    client = new WebClient();
            string json;

            try {
                json = client.DownloadString(url);
            }
            catch (System.Net.WebException e) {
                Log("Could not load JSON: " + e.Message);
                return(ServiceResultStatus.TemporaryError);
            }

            // Parse JSON and build object
            Dictionary <string, object> data;

            try {
                data = _serializer.DeserializeObject(json) as Dictionary <string, object>;
            }
            catch (Exception e) {
                Log("Could not parse JSON: " + e.Message);
                return(ServiceResultStatus.InvalidResponse);
            }

            // Check response
            if (data["Response"].ToString().ToLower() == "false")
            {
                Log(data["Error"].ToString());
                return(ServiceResultStatus.NoResult);
            }

            // Build object
            try {
                movieInfo = MovieInfo.Build(data);
                return(ServiceResultStatus.Success);
            }
            catch (Exception) {
                Log("Could not build MovieInfo object");
                return(ServiceResultStatus.InvalidResponse);
            }
        }