The Movie Database Support

I am quite familiar with C# but new to JSON. I cannot figure out how to parse the data returned by the API.

First I tried this...

        string url = "https://api.themoviedb.org/3/movie/tt1872181?api_key=" + API_KEY;

        var request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "GET";
        request.Accept = "application/json";
        request.ContentLength = 0;

        string responseContent = null;

        WebResponse response = request.GetResponse();
        //StreamReader reader = new StreamReader(response.GetResponseStream());

        StreamReader stream = new StreamReader(url);
        string text = stream.ReadToEnd();

And I DID receive the data from the API in JSON format. I could parse this data the 'old fashion' way but I know that isn't the correct way to do it nor is it efficient. So, in an effort to parse the JSON data the correct way, I changed my script to this...

        // Creates an HttpWebRequest with the specified URL. 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        // Send the request and wait for response.          
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Get the response stream
        Stream responseStream = response.GetResponseStream();
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(movie));

        object objResponse = (movie)jsonSerializer.ReadObject(responseStream);

        movie jsonResponse = objResponse as movie;

        response.Close();

Which seems to work. The error I am getting, I believe, has to do with the MOVIE class. The error I am getting is this.

An unhandled exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll

Additional information: Type 'emma.movie' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

And, finally, here is my movie class:

class movie
{
    public int Id { get; set; }
    public string ImdbId { get; set; }
    public string Title { get; set; }
    public string OriginalTitle { get; set; }
    public string Status { get; set; }
    public string Tagline { get; set; }
    public string Overview { get; set; }
    public string Homepage { get; set; }

    public string BackdropPath { get; set; }
    public string PosterPath { get; set; }

    public bool Adult { get; set; }

    public List<string> BelongsToCollection { get; set; }
    public List<string> Genres { get; set; }

    public DateTime ReleaseDate { get; set; }
    public long Revenue { get; set; }
    public long Budget { get; set; }
    public int Runtime { get; set; }

    public double Popularity { get; set; }
    public double VoteAverage { get; set; }
    public int VoteCount { get; set; }

    public List<string> ProductionCompanies { get; set; }
    public List<string> ProductionCountries { get; set; }
    public List<string> SpokenLanguages { get; set; }

    public string AlternativeTitles { get; set; }
    public string Releases { get; set; }
    public string Casts { get; set; }
    public string Images { get; set; }
    public string Keywords { get; set; }
    public string Trailers { get; set; }
    public string Translations { get; set; }
    public string SimilarMovies { get; set; }
    public string SearchContainer { get; set; }
    public List<string> Changes { get; set; }

}

Any help you can provide is greatly appreciated.

Greg

5 replies (on page 1 of 1)

Jump to last post

Hi gbgordy7@yahoo.com,

I can't offer much help with C# as I have never even opened a C# file but I am wondering, have you looked at how the fields are assigned in one of the C# libraries like this one? It looks like a field serialization issue but without knowing which field, I am not sure what it could be.

Yeah. I have looked through all of the libraries and cannot find anything to help me with my specific problem. Thank you for your input tho...I will keep searching.

That did it! Thank you so much!

I spoke too soon. This will return individual movies like when I pull up something by the IMDB ID or by the TMDB ID. But, if I do a search, it returns all NULL values. I imagine I need to return the results as a collection of movies or something similar. Can someone please point me in the right direction with this?

Thank you in advance.

Greg

You are probabyl right about the wrong collection type. You could try dynamics if you're using a newer c# version. So you can easily debug what result you get. Also check this http://james.newtonking.com/json (or better get it from nuget)

Can't find a movie or TV show? Login to create it.

Global

s focus the search bar
p open profile menu
esc close an open window
? open keyboard shortcut window

On media pages

b go back (or to parent when applicable)
e go to edit page

On TV season pages

(right arrow) go to next season
(left arrow) go to previous season

On TV episode pages

(right arrow) go to next episode
(left arrow) go to previous episode

On all image pages

a open add image window

On all edit pages

t open translation selector
ctrl+ s submit form

On discussion pages

n create new discussion
w toggle watching status
p toggle public/private
c toggle close/open
a open activity
r reply to discussion
l go to last reply
ctrl+ enter submit your message
(right arrow) next page
(left arrow) previous page

Settings

Want to rate or add this item to a list?

Login