תמיכת The Movie Database

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 תגובות (בדף 1 מתוך 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)

.לא מצאת סרט או סדרה? היכנס כדי ליצור אותם

עולמי

s התמקד בשורת החיפוש
p פתח תפריט פרופיל
esc סגור חלון פתוח
? פתח חלון קיצורי דרך של מקלדת

בדפי מדיה

b לך חזרה (או אל הורה אם ישים)
e לך אל דף עריכה

בדפי עונות

(חץ ימני) לך אל העונה הבאה
(חץ שמאלי) לך אל העונה הקודמת

בדפי סדרות

(חץ ימני) לך אל הפרק הבא
(חץ שמאלי) לך אל הפרק הקודם

בכל דפי תמונה

a פתח חלון הוספת תמונה

בכל דפי עריכה

t פתח בוחר תרגום
ctrl+ s הגש טופס

בדפי דיון

n צור דיון חדש
w עורר מעמד צפייה
p עורר ציבורי/פרטי
c עורר סגירה/פתיחה
a פתח פעילות
r השב אל דיון
l לך אל תשובה אחרונה
ctrl+ enter הגש את הודעתך
(חץ ימני) הדף הבא
(חץ שמאלי) הדף הקודם

הגדרות

?רוצה לדרג או להוסיף פריט אל רשימה

היכנס